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

2.7k Views Asked by At

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.

5

There are 5 best solutions below

0
NhanTT On BEST ANSWER

I just remove --incognito It working

0
TechnoWolf On

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.)

1
Karthik Rajan On

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);
2
Andy On

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')
0
Brilliant-DucN On

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"]},
}