Are there any headless browsers compatible with Selenium that support downloading files?

I need to automate the downloading of a file using Selenium for my tests. The webpage utilizes JavaScript, making it unsuitable for HttpClient with HttpState. I’ve learned that PhantomJs doesn’t have this capability, particularly on Linux systems. Currently, I’m using Firefox with the following code, though it’s not running in headless mode.

FirefoxProfile profile = new FirefoxProfile();
String downloadPath;
if (SystemUtils.IS_OS_LINUX) {
    downloadPath = "/tmp/files/";
} else {
    downloadPath = "C:\Files\";
}
profile.setPreference("browser.download.dir", downloadPath);
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("pdfjs.disabled", true);
WebDriver driver = new FirefoxDriver(profile);

Is there a way to achieve similar functionality with a headless browser?

For automating downloads in headless mode with Selenium, Chrome in headless mode is a viable option. While Firefox in headless mode currently has limitations regarding file downloads, Chrome provides a robust solution that can handle JavaScript-heavy pages effectively. Below is a well-configured approach using ChromeOptions to manage downloads without any dialog. Here’s how you can set it up in Java:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.util.HashMap;
import java.util.Map;

public class HeadlessDownload {
    public static void main(String[] args) {
        String downloadFilepath = SystemUtils.IS_OS_LINUX ? "/tmp/files" : "C:\\Files";

        Map<String, Object> prefs = new HashMap<String, Object>();
        prefs.put("download.default_directory", downloadFilepath);
        prefs.put("download.prompt_for_download", false);
        prefs.put("download.directory_upgrade", true);
        prefs.put("plugins.always_open_pdf_externally", true);

        ChromeOptions options = new ChromeOptions();
        options.setHeadless(true);
        options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200");
        options.setExperimentalOption("prefs", prefs);

        WebDriver driver = new ChromeDriver(options);
        driver.get("YOUR_DOWNLOAD_LINK_HERE");

        // Add your logic to locate and click the download button

        driver.quit();
    }
}

Key Configuration:

  • download.default_directory sets the directory where files are saved.
  • download.prompt_for_download is disabled to ensure downloads proceed without additional prompts.
  • plugins.always_open_pdf_externally disables PDF plugins for external handling via download.

By implementing these settings, you can leverage Chrome in headless mode to facilitate file downloads, making it a versatile choice for your automation scripts.