Search code examples
selenium-chromedriver

Selenium ChromeDriver: how to trigger PDF download insted of PDF display?


I have a test-suite based on Selenium's ChromeDriver which as part of some tests triggers a PDF download from the server. This has worked fine so far but now breaks because newer Chrome versions want to display the PDF using the built-in PDF viewer instead of downloading it to a local file. I found out that I can change that behavior interactively by opening Chrome's settings (via the URL "chrome://settings/content/pdfDocuments" and then selecting the upper radio-button "Download PDFs" instead of the lower radio-button "Open PDFs in Chrome" (which is set by default). However, this config change then only applies when I start Chrome interactively. This modified setting is not kept (or not applied) when I start Chrome via the Selenium Chrome driver.

How can I modify that setting when using Chrome via the Selenium Chrome driver? I have tried to do the same config change programmatically via ChromeDriver by initially calling the same config-URL mentioned above but I am unable to locate the "Download PDFs" radio-button on the config page. That button has no specific class and trying to locate it via XPath or the contained text has always failed on me so far.

Is there some ChromeDriver Guru that has experience with this or an idea how to modify Chrome's config when controlling it via the Selenium ChromeDriver?

I am using Chrome Version 131.0.6778.70 (Official Build) (64-bit)

Selenium and the ChromeDriver versions are both v4.21.0.

Addition: I tried to use this code:

        ...
        ChromeOptions chromeOptions = new ChromeOptions(); 
        chromeOptions.addArguments("profile.default_content_settings.popups", "0"); // disable the popup window
        chromeOptions.addArguments("download.default_directory", "U://Downloads"); // Change default directory for downloads
        chromeOptions.addArguments("download.prompt_for_download", "false");
        chromeOptions.addArguments("download.directory_upgrade", "true");
        chromeOptions.addArguments("plugins.always_open_pdf_externally", "false"); // It will not show PDF directly in chrome 
        driver = new ChromeDriver(chromeOptions);
        ...

... as suggested in the append referenced by "K J" but that did not work. The PDF is still displayed directly in Chrome's PDF preview and not downloaded.


Solution

  • For anyone stumbling over this thread:

    The following did the trick: one needs to set these options not via chromeOptions.addArguments(<option-name>, <option-value>); but via a HashMap (here named chromeOptionsMap ) into which one adds the option(s) using chromeOptionsMap.put(<option-name>, <option-value>); and which one then adds as argument using chromeOptions.setExperimentalOption("prefs", chromeOptionsMap);, i.e. like so:

        ...
        // Initialize ChromeDriver:
        // found this at "https://medium.com/@akshayshinde7289/how-to-download-pdf-file-in-chrome-using-selenium-6a717ced483b" 
        // to disable the built-in PDF previewer:
        ChromeOptions chromeOptions = new ChromeOptions();
        HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
        chromeOptionsMap.put("download.default_directory", DownloadDirPath);
        chromeOptionsMap.put("plugins.plugins_disabled", new String[] { "Chrome PDF Viewer" });
        chromeOptionsMap.put("plugins.always_open_pdf_externally", true);
    
        chromeOptions.setExperimentalOption("prefs", chromeOptionsMap);
        driver = new ChromeDriver(chromeOptions);
        ...
    

    That then finally got me going...