Set Firefox profile to download files automatically using Selenium and Java

The way it currently works in Firefox 57.0b13 is

FirefoxProfile profile = new FirefoxProfile();
// profile.setPreference("browser.download.useDownloadDir", true); This is true by default. Add it if it's not working without it.

profile.setPreference("browser.download.folderList",2); //Use for the default download directory the last folder specified for a download
profile.setPreference("browser.download.dir", "/Path/to/directory"); //Set the last directory used for saving a file from the "What should (browser) do with this file?" dialog.
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf"); //list of MIME types to save to disk without asking what to use to open the file
profile.setPreference("pdfjs.disabled", true);  // disable the built-in PDF viewer

firefoxOptions.setProfile(profile);

Detailed info about each Firefox profile setting


It is 2020 now. Find MIME type as @Florent B. mentioned above. For me, download csv file and found that Content-Type = "application/octet-stream"

To download to folder Downloads:

FirefoxOptions options = new FirefoxOptions();
options.addPreference("browser.download.folderList",1);
options.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");
WebDriver driver = new FirefoxDriver(options);

To download to desktop, change the value in 2nd line to 0:

FirefoxOptions options = new FirefoxOptions();
options.addPreference("browser.download.folderList",0);
options.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");
WebDriver driver = new FirefoxDriver(options);

To download to another folder:

FirefoxOptions options = new FirefoxOptions();
options.addPreference("browser.download.dir", "D:\\Test");
options.addPreference("browser.download.folderList",2);
options.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");
WebDriver driver = new FirefoxDriver(options);

Just like @Jason suggested, it's most probably another mime type. To get the mime type:

  • Open Developer Tools
  • Go to Network
  • Click on the link to download the pdf
  • In the network panel, select the first request
  • The mime type is the Content-Type from the response header:

enter image description here

Then to download a PDF with Firefox:

FirefoxOptions options = new FirefoxOptions();
options.setPreference("browser.download.folderList", 2);
options.setPreference("browser.download.dir", "C:\\Windows\\temp");
options.setPreference("browser.download.useDownloadDir", true);
options.setPreference("browser.download.viewableInternally.enabledTypes", "");
options.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf;text/plain;application/text;text/xml;application/xml");
options.setPreference("pdfjs.disabled", true);  // disable the built-in PDF viewer

WebDriver driver = new FirefoxDriver(options);
driver.get("https://www.mozilla.org/en-US/foundation/documents");
driver.findElement(By.linkText("IRS Form 872-C")).click();

If anyone is having this issue within a SPA environment, then I hit an issue where the setting the saveToDisk preference to the expected content type didn't work (in my case text/csv).

The reason why is the SPA UI initiates a HTTP call to the backend api to get the CSV data. It then does a trick to create an <A> element which it clicks to initiate the download to the local machine. The trick creates a Blob object with the CSV data and type must be set to application/octet-stream as part of it. Therefore the saveToDisk must also be set to application/octet-stream for this to work.