Set Firefox profile to download files automatically using Selenium and Java

I want to verify file download using Selenium WebDriver and Java. The file to download is of PDF format. When WebDriver clicks on "Download" link in the AUT, Firefox opens up the following download confirmation window:

Download Confirmation Window

I want Firefox to download the file automatically without showing above confirmation window, so I used the below code:

FirefoxProfile firefoxProfile=new FirefoxProfile();
firefoxProfile.setPreference("browser.download.folderList",2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
firefoxProfile.setPreference("browser.download.dir",downloadPath);
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf");
WebDriver driver=new FirefoxDriver(firefoxProfile); 

but still Firefox shows the same window. How can I set Firefox profile so that PDF files are downloaded automatically without showing the confirmation dialogue?


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();

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


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.


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

I would write this as a comment, but I don't have enough reputation points--once the selenium webdriver is launched you can navigate to about:config and search for browser.helperApps.neverAsk.saveToDisk to confirm that the types you specific were properly recorded.

In my case the issue was resolved by also including

prof.set_preference("browser.helperApps.neverAsk.openFile", "application/pdf, application/octet-stream, application/x-winzip, application/x-pdf, application/x-gzip")