Install a Firefox profile to automatically download files using Selenium and Java

I want to check file uploads using Selenium WebDriver and Java. PDF file to download. When WebDriver clicks on the “Download” link in AUT, Firefox opens the following download confirmation window:

Download Confirmation Window

I want Firefox to download the file automatically without showing the confirmation window above, so I used the following 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 Firefox still displays the same window. How do I set up a Firefox profile so that PDF files load automatically without showing a confirmation dialog?

+19
java selenium-webdriver
source share
4 answers

As suggested by @Jason, this is most likely a different type of mime. To get the MIME type:

  • Open Developer Tools
  • Go to the 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 the PDF from 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.helperApps.neverAsk.saveToDisk", "application/pdf"); 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(); 
+35
source share

It currently works in Firefox 57.0b13

 FirefoxProfile profile = new FirefoxProfile(); // profile.setPreference("browser.download.useDownloadDir", true); This is true by default. Add it if it 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); 

Details of Each Firefox Profile Setting

+3
source share

If someone had this problem in the SPA environment, I ran into a problem when setting the saveToDisk preference for the expected content type did not work (in my case text/csv )

The reason the SPA user interface initiates an HTTP call to the backend API to receive CSV data. He then does the trick to create the <A> element that he clicks to start downloading to the local computer. The trick is creating a Blob object with CSV data, and octet/stream must be set for its type. Therefore saveToDisk must also be set to octet/stream for this to work.

+2
source share

I would write this as a comment, but I do not have enough reputation points - after starting the selenium web driver, you can go to about: config and search for browser.helperApps.neverAsk.saveToDisk to confirm that the types you specified are were correctly recorded.

In my case, the problem was resolved by including

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

All Articles