0

My problem Statement is:
I want to download PDF using JavaScriptExecutor, When I click on the
PDF link it opens the PDF in a new window, It is an online PDF Viewer window, having a toolbar section where print, download..etc, features are available, Manually I can download by clicking on Download button. Now I want to automate this Scenario, I Researched and found that it can be
handled by using "javascript executor" I inspect that opened PDF window and the toolbar section is inside nested shadow root. Then I proceeded with writing the JSPath of the download element, basically, I copied the JSPath of the download Element, I checked it on the Console and it was able and locate and perform the Click operation. Same when I tried to do it through Script it gives me the exception "Cannot read properties of null(reading 'ShadowRoot')" Screenshots of DevTool Console

The code I used so far:

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

Does anyone have any possible solutions or insight into How to download the PDF? I'm relatively new to programming and would appreciate any advice. Please let me know, If any other information required.. Thanks in advance

  • Thanks for the Info, I understand your point, The Issue is I am not able to download the PDF, I have tried the Rest Client method as there was a backend change similar to https://stackoverflow.com/questions/74080201/is-there-a-way-to-download-pdf-through-a-link-using-selenium-c-sharp – vivek joshi Oct 18 '22 at 06:03
  • I tried Ctrl+S, but it's saving it as an HTML file and still loading Past 10 mins, I need it as a PDF, then I also tried Ctrl+P it's not capturing the full PDF. – vivek joshi Oct 18 '22 at 11:04
  • No @KJ the focus is on the PDF viewer window, When I do Ctrl+ S it gives me 3 options to save which are: (Webpage, Single file Webpage, HTML Only Webpage, Complete)I have Checked Manually also, but it's not saving it as a PDF. – vivek joshi Oct 20 '22 at 10:21
  • Is there any Modifications, I can implement with JavaScript Executor – vivek joshi Oct 20 '22 at 10:26
  • Checked Manually, getting the same 3 options after clicking on save as – vivek joshi Oct 25 '22 at 15:18

1 Answers1

0

You could just get the url of the pdf link, then download it using an HTTP client.

I'm not familiar with C#, but here's pseudocode:

var url = driver.findElement(pdflink).getAttribute(href);
var client = new HttpClient();
var result = client.Get(url);
Files.write("myfile.pdf", pdf);
functorial
  • 330
  • 3
  • 13
  • I tried Rest Client, but my problem is similar to https://stackoverflow.com/questions/74080201/is-there-a-way-to-download-pdf-through-a-link-using-selenium-c-sharp, then I thought of going to 'JavaScriptExecutor' As the end goal is to download the PDF – vivek joshi Oct 18 '22 at 05:16
  • 1
    @vivekjoshi from your comments, it sounds like the pdf is displayed within an iframe, in which case this answer might help https://stackoverflow.com/questions/56986848/how-to-download-embedded-pdf-from-webpage-using-selenium. Otherwise, I think we'd need to see the url to give any more advice. – functorial Oct 19 '22 at 04:21
  • No @functorial my PDF is not within Iframe. var URL = driver.findElement(pdflink).getAttribute("href"); – vivek joshi Oct 20 '22 at 07:23
  • The URL I am getting is a GET request URL that URL I can see in-network tab also The response body of the GET request contains some scripts that are internally calling post requests along with the payload provided. – vivek joshi Oct 20 '22 at 07:32
  • And those post request are responsible for loading the PDF. – vivek joshi Oct 20 '22 at 07:33
  • 1
    Again, that would indicate that you don't have the actual URL of the pdf. – functorial Oct 22 '22 at 00:14
  • I do have the URL, but the URL is not specific, it changes according to the input we provide, it's a very long URL depending on the input. URL: https://www.example.com/baseEndPoint/xyz/generate-presentation.htm?id=dgfd&filename=test_pdf&client=&description=&profileId=7952665&profileTypeId=8&shareClassTypeId=shareClass&customFullName=&customFirm=&address1=&address2=&city=&state=&zip=&phone=&phoneExt=&email=&createPresentation=Create&pdfPluginInstalled=false – vivek joshi Oct 25 '22 at 15:05
  • When I am using the Rest Client method to download PDF, the pdf is downloaded at the desired location but it's corrupt giving me an error message while opening the downloaded pdf "Something went wrong please refresh the page" – vivek joshi Oct 25 '22 at 15:12