ChromeDriver Version 117+ Forces "Save As" Dialog - How to Bypass? (Selenium/Java)
Asked Answered
E

5

8

I have been working on automating file downloads using Selenium WebDriver with ChromeDriver in Java. My code was working perfectly until I updated to ChromeDriver version 117+, the code worked fine till Chrome 116.0.5845.141, problem seems to start in Chrome 116.0.5845.188. Now, it seems that the browser is forcing the "Save As" dialog box to appear, even when I have set Chrome preferences to avoid it.

Here is a snippet of my Java code:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.HashMap;

public class FileDownloadHeadless {
    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");

        ChromeOptions options = new ChromeOptions();
        options.setCapability("os", "Windows");
        options.setCapability("os_version", "10");
        options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        options.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
        options.setCapability("chrome.switches", Arrays.asList("--incognito"));
        options.setCapability(ChromeOptions.CAPABILITY, options);
        options.addArguments("--headless");
        options.addArguments("--disable-gpu");

        HashMap<String, Object> chromePrefs = new HashMap<>();
        chromePrefs.put("profile.default_content_settings.popups", 0);
        chromePrefs.put("download.default_directory", "C:\\local_files");
        chromePrefs.put("download.prompt_for_download", false);
        chromePrefs.put("profile.content_settings.exceptions.automatic_downloads.*.setting", 1);
        chromePrefs.put("profile.default_content_setting_values.automatic_downloads", 1);
        options.setExperimentalOption("prefs", chromePrefs);

        WebDriver driver = new ChromeDriver(options);

        driver.get("http://my_site.com/download");

        driver.findElement(By.id("id_button_download")).click();

        Thread.sleep(5000);

        driver.quit();
    }
}

Despite these settings, the "Save As" dialog box still appears, and it's disrupting the automation flow. I've tried various combinations of Chrome preferences, but none seem to bypass the new behavior introduced in version 117.

Has anyone else encountered this issue with ChromeDriver version 117+ or higher? If so, how have you managed to work around this update? Any insights would be greatly appreciated.

Removing the "--incognito" mode as suggested by @NhanTT, did work! but I wonder why and how the mode actually affects file download options. If anyone has another solution that works around the problem while maintaining incognito mode, I would appreciate it.

Thank you for your time and assistance.

Epperson answered 12/9, 2023 at 23:42 Comment(0)
W
4

I just remove --incognito It working

Warrington answered 13/9, 2023 at 6:36 Comment(0)
A
2

If you need to keep Incognito mode enabled, you can avoid the Save As dialog with the following (in Python).

options = ChromeOptions()
options.add_argument('incognito')
options.add_argument('disable-features=DownloadBubble,DownloadBubbleV2')
Aqualung answered 5/10, 2023 at 23:45 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Musing
This workaround used to work for me in Chrome 117, but now in Chrome 120 I think this no longer works.Ultramarine
P
0

Solution: If you're like me, you need to launch chrome in incognito. In my case it's to force a different login, so simply removing --incognito is not an option.

As of Chrome 117, all downloads in incognito mode will force the Save As dialog box regardless of the Chrome Prefs you set.

(Python Solution - but you should be able to translate to Java easily enough)

To fix this, we need to set two flags. you can do so by shoving the following code in right under your chromeOptions.add_argument('--incognito'):

chromeLocalStatePrefs = {'browser.enabled_labs_experiments': ['download-bubble@2', 'download-bubble-v2@2']}
chromeOptions.add_experimental_option('localState', chromeLocalStatePrefs)

(be sure "chromeOptions" matches whatever your Chrome options variable is: chromeOptions = webdriver.ChromeOptions(). chrome_options is commonly used, for example.)

Protectionist answered 20/9, 2023 at 18:53 Comment(0)
D
0

To execute in incognito mode, add the below lines along with your code(Java).

            List<String> enabledLabsExperiments = new ArrayList<>();
            enabledLabsExperiments.add("download-bubble@2");
            enabledLabsExperiments.add("download-bubble-v2@2");
            chromePrefs.put("browser.enabled_labs_experiments", enabledLabsExperiments);
            options.setExperimentalOption("localState", chromePrefs);
Delozier answered 25/9, 2023 at 9:15 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Musing
F
0

For people using Serenity-BDD, let add the below option to your serenity.conf

"goog:chromeOptions" {
  ...
  ...
  // Fix: Chrome 117 forces "Save As" dialog to appear
  localState = {"browser.enabled_labs_experiments": ["download-bubble@2", "download-bubble-v2@2"]},
}
Flycatcher answered 7/10, 2023 at 15:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.