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?

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
stackoverflow
  • 2,134
  • 3
  • 19
  • 35
  • 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");` – JRodDynamite Mar 30 '16 at 12:56
  • JRodDynamite is correct, you need to put them all on one line, or it will only take the last one. – user890332 Dec 17 '19 at 15:57

8 Answers8

61

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();
blaz
  • 314
  • 4
  • 9
Florent B.
  • 41,537
  • 7
  • 86
  • 101
  • 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 – stackoverflow Mar 31 '16 at 04:10
  • Can you provide a public PDF link that reproduce your issue. You can easily get one by searching "pdf filetype:pdf" in Google. – Florent B. Mar 31 '16 at 08:57
  • You can check for any pdf file in the link [https://www.ibm.com/developerworks/community/files/app#/] – stackoverflow Mar 31 '16 at 09:43
  • 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. Mar 31 '16 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 Sep 09 '17 at 02: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 Oct 06 '17 at 07:58
  • @FlorentB. Thanks really a lot, I was going nut as an excel file was seen as application/download (as it was a javascript lunching the download) – tmow Aug 23 '18 at 15:54
  • now it is `options.SetPreference(...)` with capital S. – LoaStaub Nov 14 '19 at 10:44
  • Is it possible to allow for different file type and not just `pdf` ? – CJ7 Jul 13 '20 at 05:49
  • 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 Oct 07 '20 at 10:45
  • Some companies, due to security issues might have disabled the preference "browser.helperApps.neverAsk.saveToDisk" using the "PromptForDownloadLocation": true (policies.json). That means whatever you put in the mime-types, it will not work. – Ap Tsi Oct 30 '20 at 11:07
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

7cart project
  • 317
  • 1
  • 4
  • 13
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.

Sepia
  • 447
  • 6
  • 21
deejbee
  • 1,148
  • 11
  • 17
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);
hthhth
  • 111
  • 1
  • 5
  • 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 Mar 08 '22 at 04: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")
Bjc
  • 93
  • 6
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'
Daniel Butler
  • 3,239
  • 2
  • 24
  • 37
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! :)

YaAC
  • 1
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);
Sunny
  • 89
  • 1
  • 9