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