1
votes

Let me begin by saying I have tried suggestions from the following linked Stack Overflow articles to no avail:

Here is what I have for my ChromeOptions:

            {
                ChromeOptions chromeOptions = new ChromeOptions();
                chromeOptions.AddUserProfilePreference("download.prompt_for_download", false);
                chromeOptions.AddUserProfilePreference("download.directory_upgrade", true);
                chromeOptions.AddUserProfilePreference("download.default_directory", DOWNLOAD_DIR);
                //chromeOptions.AddUserProfilePreference("disable-popup-blocking", true);
                chromeOptions.AddUserProfilePreference("plugins.plugins_disabled", "Chrome PDF Viewer");
                chromeOptions.AddUserProfilePreference("plugins.always_open_pdf_externally", true); 
                chromeOptions.AddArgument("--no-sandbox");
                _driver = new ChromeDriver(chromeOptions);
            }

You'll notice the ("disable-popup-blocking", true) bit is commented out, and this is because it took me from seeing this:

disabled PDF viewer

back to the PDF desired for download appearing.

Like all of the above linked posts, I need to download the PDF when I click the link to it instead of opening it in a new window, and because of Selenium's limitations with PDFs, I have not been able to successfully click the "Open" button on the disabled viewer. Visual Studio throws the following exception:

NoSuchElementException

As requested in the comments, here's the code attempting to interact with the disabled PDF viewer:

_driver.SwitchTo().Window(_driver.WindowHandles[1]);
_driver.FindElement(By.LinkText("Open")).Click();

Does anyone have any possible solutions or insight into what is going on here? I'm still relatively new to programming and would appreciate any advice.

In case it's at all relevant, the PDF I'm attempting to download is a Salesforce Lightning PDF

2
Please include the code in your question.Daniel Manta
@derloopkat I updated the question with the code specifically attempting to interact with the disabled PDF viewer.paris

2 Answers

0
votes

NoSuchElementException means your element is not present on DOM.

you have to change By.LinkText("Open") to actual button's selector.

0
votes

Update: I finally found the solution, so I'm posting in case anyone else is encountering this problem.

The problem here has to do with the button to click the PDF being inside an iFrame(see the "disabled PDF viewer" image linked above). All the aforementioned ChromeOptions are correct, as is the code attempting to interact with the disabled PDF viewer. What's missing is this line:

_driver.SwitchTo().Frame("pdfFrame");

which would go in between the two lines listed above, resulting in 3 lines of code that look like this:

 _driver.SwitchTo().Window(_driver.WindowHandles[1]);
 _driver.SwitchTo().Frame("pdfFrame");
 _driver.FindElement(By.LinkText("Open")).Click();

After adding this, I'm able to interact with the Open button and my PDF downloads to my desired directory flawlessly.