Chrome headless mode prevents file downloads from working

I’m having trouble with file downloads when running Chrome in headless mode. The download feature works perfectly when I run the browser normally, but as soon as I switch to headless mode, no files appear in my target folder after clicking download buttons.

Here’s my current browser configuration:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();

Map<String, Object> preferences = new HashMap<String, Object>();
preferences.put("profile.default_content_setting_values.notifications", 2);
preferences.put("profile.default_content_settings.popups", 0);
preferences.put("download.default_directory", System.getProperty("user.dir")+File.separator+"downloads");

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless", "window-size=1280,720", "--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
options.setExperimentalOption("prefs", preferences);
options.addArguments("disable-popup-blocking");
options.addArguments("--disable-extensions");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);

What am I missing to make downloads work in headless Chrome?

i had this prob too a while back. adding --allow-running-insecure-content and --disable-web-security helped me out. just double check ur download folder is there b4 u start the browser. goodluck!

Chrome blocks downloads in headless mode by default - that’s your problem. You’ll need to use Chrome DevTools Protocol to enable them programmatically. Run this command right after initializing your driver:

((ChromeDriver) driver).executeCdpCommand("Page.setDownloadBehavior", 
Map.of("behavior", "allow", "downloadPath", System.getProperty("user.dir")+File.separator+"downloads"));

This forces headless Chrome to allow downloads and sets where they’ll go. Had the exact same issue last year and this CDP command fixed it instantly. Just make sure your ChromeDriver version supports CDP commands.

You’re missing the download behavior config for headless Chrome. Add “profile.default_content_settings.automatic_downloads”: 1 to your preferences - this stops Chrome from showing download confirmation dialogs that break in headless mode. Also, ditch the old DesiredCapabilities approach since it’s deprecated. Just use ChromeOptions directly with new ChromeDriver(options). I’ve seen download settings that only work with the newer ChromeOptions setup, so mixing the two can cause issues.