``Is it feasible to extract a PDF file using a PDF viewer in selenium using C# programming language

Facing an issue with downloading PDF files using JavaScriptExecutor:
I am trying to automate the process of downloading a PDF file in a new window using JavaScriptExecutor. The online PDF viewer window has a toolbar section with options like print and download. Manually, I can click on the download button to save the PDF. However, when I tried to automate this process by inspecting the nested shadow root element, I encountered an exception "Cannot read properties of null(reading 'ShadowRoot')". I have attached screenshots of the DevTool Console for reference. Screenshots of DevTool Console

Here is the code snippet I used:

IWebDriver driver;
driver.FindElement(By.Xpath("Xpath_of_PDF_Link")).Click();
driver.SwitchTo().Window(driver.WindowHandles.Last());
Thread.Sleep(2000);
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
IWebElement downloadButton = (IWebElement)js.ExecuteScript("return document.querySelector(\"#viewer\").shadowRoot.querySelector(\"#toolbar\").shadowRoot.querySelector(\"#downloads\").shadowRoot.querySelector(\"#download\")");
Thread.Sleep(1000);
var element = "argument[0].click()";
js.ExecuteScript(element,downloadButton);
        

Screenshots of devTool attached below

Looking for suggestions or solutions on how to resolve this issue. Any insights would be helpful as I am new to programming. Feel free to ask for any additional information if needed. Thank you in advance.

Answer №1

To download a PDF file, you can simply retrieve the URL of the PDF link and use an HTTP client to fetch it.

If you're not experienced with C#, here is some pseudo code that may help:

var pdfUrl = driver.findElement(pdflink).getAttribute(href);
var httpClient = new HttpClient();
var pdfFile = httpClient.Get(pdfUrl);
Files.write("downloaded_file.pdf", pdfFile);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a default menu within a specific route in Ember.js is a crucial step in

I have created a JSBin code for my Ember.js application which can be found here: When I click on Item A, I want the ABCD menu on the left to remain visible and not disappear. Thank you. ...

Trouble with using $.ajax to call a PHP function

Hey everyone, I hate to ask for help but I'm really stuck on this issue. I've tried everything and followed all the scenarios on this site, but I just can't seem to get it working. I even added alerts and echoes, but the function doesn' ...

AngularJS is not displaying the interface changes as expected

I have a list that is unordered, and whenever I click on a ListItem, it triggers the setActive() function. <li ng-repeat="slide in data" ng-click="setActive(slide.slideIndex)"> <span class="listItemText"> <b>{{slide.slideIndex + 1} ...

Incorrect rendering of the <li> tag

I've been working on creating a simple web "to do list" but I've encountered an issue. When I manually add todos and click on them, the 'text-decoration: line-through;' property is ignored, and I can't see the strikethrough effect ...

When reloading the Javascript React application, the "fetch" function is returning 'undefined' in the browser. However, when the application is reloaded directly from the API, it successfully

I am currently working on a component that retrieves a JSON object from GitHub using an asynchronous function. Here is the code snippet: async function getQueryJson(URL) { await fetch(URL) // This gets the git commits .then((res) => res.json()) // ...

struggling with handling form data in Express/Node application

Currently this is my setup: Below is the Post method I am using app.post('/barchartentry', function(req, res){ res.render('barchart', { title: 'EZgraph: Bar Chart', barValues: req.body.myInputs, labelValues: req.body ...

Ways to ensure the axios call is completed before proceeding with the codeexecution

I need to use axios to send a request and retrieve some data that I want to pass as a prop to a React Component. Here's my render function: render() { boards = this.fetchBoardList(); return ( <div className="app-wrapper"> ...

Using Vue/Nuxt.js to compute the cumulative sum of hierarchically structured JSON data

When using Nuxt async data, I am retrieving a JSON object that includes a nested array in the following structure: "topic_list": { "topics": [ { "id": 9148, "title": "A", "views": 12 }, { "id": 3228, ...

Adding vertices to a vertex buffer that has already been initialized in WebGL

My journey into learning WebGL has led me to initialize a vertex buffer with data that is designated for gl.STATIC_DRAW. According to the documentation on MDN, gl.STATIC_DRAW is typically used when the vertex data remains constant throughout the applicatio ...

The mobile menu functions correctly on Jfiddle but is not functioning on the local server

I was working on a responsive mobile menu and used a toggleClass function to open the menu. It's functioning correctly in Jfiddle and below, but every time I click the nav icon in the live preview within brackets, nothing happens. I even tried it in a ...

What is causing the delay() function to malfunction?

Check out this example: $('#click').click(function() { $('#delay').delay(2000).css('background-color', '#c30000'); }); Can you explain why the delay() function does not delay the execution of the css() function i ...

Encountering a "Selenium error: element not interactable exception" when attempting to use sendkeys on a Google Form?

I've been attempting to automate filling out a Google Form using Selenium in Python 3.9. https://docs.google.com/forms/d/e/1FAIpQLSead7qqhVWP4m4q9Y71Wu9kr2lbCQXvY4ge0OdFg9fI0SQYYA/viewform Unfortunately, I encountered an error mentioning "element no ...

How can we enable Material UI tooltip based on certain conditions?

In my React component utilizing Material UI, I have the code snippet below: const MyButton = ({ warningText }) => ( <Tooltip title={warningText}> <Button>Do action</Button> </Tooltip> ) At present, an empty tool ...

problem encountered with browsermob-proxy when using webdriverio to generate har files

While attempting to utilize browsermob-proxy with webdriverio using this and this, everything seems to be functioning correctly, but the expected har file is not being generated. I made an adjustment to the code snippet below: fs.writeFileSync('stuff ...

Empty value for $_POST variable following xmlhttp request

When my code makes an xmlhttp request to a php file with an ID for record deletion from the database, I encounter an issue. The error message 'comicID' is undefined shows up when attempting to delete. This signifies that the variable containing t ...

Utilizing JavaScript Array Mapping with Object Properties

I am facing a challenge with my dynamic rendering component, which is responsible for rendering sub-components. I recently encapsulated the component within another object, and I am struggling to rewrite the rendering process. Rendering {this.props.compo ...

When I click on .toggle-menu, I want the data-target to have a toggled class and the other divs to expand

Looking to achieve a functionality where clicking on .toggle-menu will toggle a class on the specified data-target element and expand other divs accordingly. jQuery(document).ready(function() { var windowWidth = jQuery(window).width(); if (win ...

How to programmatically unselect an ng-checkbox in AngularJS when it is removed from an array

Utilizing checkboxes to gather values and store them in an array for dataset filtering purposes. The objectives are as follows: Show child filters when parent category is selected. Unselect all children if parent is unselected (otherwise they will ...

Difficulty with Angular JS` ng-repeat functionality

I attempted to create a sample similar to the ones found on https://angularjs.org/ Here is the JS fiddle I put together. Could someone provide assistance? What seems to be the issue? The HTML code is as follows: <h2> To Do </h2> <div ng- ...

Display HTML content retrieved from a string saved in a React database

I am currently in the process of creating a React page and facing a challenge with incorporating HTML content from a RTDB that has been styled using "use styles". Here is an example of the styling: import { makeStyles } from '@material-ui/core/style ...