Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 221
stackoverflow Avatar asked Mar 30 '16 12:03

stackoverflow


People also ask

How do I enable automatic downloads in Firefox?

Chosen SolutionClick the Firefox button, go to Options | Options | General and in the Downloads menu, checkmark the option "Always ask me where to save files". Click the Firefox button, go to Options | Options | General and in the Downloads menu, checkmark the option "'''Always ask me where to save files'''".

What is the code to use the Firefox profile with desired capabilities?

FirefoxOptions options = new FirefoxOptions(); FirefoxProfile profile = new FirefoxProfile();


1 Answers

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(); 
like image 175
Florent B. Avatar answered Oct 09 '22 03:10

Florent B.