34

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?

2
  • 6
    What is the mime-type of the response? Give this a try : firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf,application/x-pdf,application/octet-stream"); Commented Mar 30, 2016 at 12:56
  • JRodDynamite is correct, you need to put them all on one line, or it will only take the last one.
    – user890332
    Commented Dec 17, 2019 at 15:57

8 Answers 8

62

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();
11
  • The mime type is application/pdf. I can see a message in developer tab console 'Resource interpreted as Document but transferred with MIME type application/pdf'. Is this message has any relation with the issue @Jason Commented Mar 31, 2016 at 4:10
  • 1
    I've updated the answer with an example where the built-in viewer is disabled. Let me know if it works for you.
    – Florent B.
    Commented Mar 31, 2016 at 16:22
  • 2
    I had to use all of the options listed in this answer plus one more: profile.setPreference("browser.download.useDownloadDir", true);
    – bparry
    Commented Sep 9, 2017 at 2:34
  • 5
    FirefoxDriver(profile) is marked as deprecated now. One can use instead FirefoxOptions options = new FirefoxOptions(); options.setProfile(profile); WebDriver driver = new FirefoxDriver(options); instead
    – ptstone
    Commented Oct 6, 2017 at 7:58
  • 2
    Since Firefox 81 you also have to clear preference browser.download.viewableInternally.enabledTypes. Otherwise file types no that list won't download without user interaction.
    – blaz
    Commented Oct 7, 2020 at 10:45
8

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

3

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.

1
  • 3
    I'm sure you meant application/octet-stream not octet/stream
    – tonysepia
    Commented Oct 3, 2018 at 13:11
3

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);
1
  • Yes! Thank you! I was using "application/zip" when trying to download some zip files, but the server wasn't sending that mimetype. I changed this to "application/octet-stream" (i.e., a generic mimetype) and it worked!
    – farenorth
    Commented Mar 8, 2022 at 4:54
2

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")
1

In case you are like me and looking at this for other file types or multiple types. Make sure you only set the neverAsk preference once.

opts.set_preference('browser.download.folderList', 2)
opts.set_preference('browser.download.manager.showWhenStarting', False)   opts.set_preference('browser.download.dir', str(download_directory))    opts.set_preference('browser.download.useDownloadDir', True)

# important part!
opts.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/zip')

I'll gladly update this if people know how to include more than one mime type!

Other mime types for quick reference:

# CSV mimetype = 'text/csv'
# TXT mimetype = 'text/txt'
# EXCEL mimetype = 'application/vnd.ms-excel'
1
  • 1
    This is/was the answer for me. "neverAsk" preference once. if I could have those hours back.
    – grantiago
    Commented May 19, 2021 at 0:15
0

Err, according to JRodDynamite's answer:

opts.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/zip,text/csv,text/txt')

should do the trick for multiple mime types - in this case just for either zip, csv & txt files.

Personally, (and the reason I'm on this page right now) I find it puzzling that the 'just save the #$&*!' option is not the default for 'unknown' - aka 'application/octet-stream' - file types. Oh, well, such is life! :)

0
    WebDriver driver;
//For firefox
    FirefoxOptions options = new FirefoxOptions(); //For setting up options for Firefox
    DesiredCapabilities firefoxDesiredCapabilities = new DesiredCapabilities(); //Initializing desired capabilities
    options.addPreference("browser.download.folderList", 2); //Last downloaded folder
    options.addPreference("browser.download.dir", "[path]"); // Set your default download directory's path
    options.addPreference("browser.privatebrowsing.autostart", true); 
    options.addPreference("browser.helperApps.neverAsk.saveToDisk", "text/plain; charset=utf-8; text/csv;application/json;charset=utf-8;application/pdf;text/plain;application/text;text/xml;application/xml"); //includes a varied list of context-type but feel free to add others
    options.addPreference("pdfjs.disabled", true);
    options.addArguments("-private");//for incognito
    firefoxDesiredCapabilities.setCapability("moz:firefoxOptions",options); //Load all options to desired capabilities
    driver = new FirefoxDriver(firefoxDesiredCapabilities); //launch your driver using desiredcapalities

//For Chrome

ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 0);//Don't display any popup for download
prefs.put("download.default_directory", "[path]");//Default download directory
options.setExperimentalOption("prefs", prefs);
options.addArguments("--incognito");
DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();
desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
desiredCapabilities.setCapability("applicationCacheEnabled", false);
driver = new ChromeDriver(desiredCapabilities);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.