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.