Selenium Webdriver Handle Firefox Or Chrome Download Popup

When you use Firefox to download a zip or pdf file in selenium webdriver automation test scripts, you may find a dialog popup which you can not handle always. When you download a pdf use Chrome browser, you may find that it is opened in browser tab instead of being downloaded. In this article, we will tell you how to resolve such issues by example.

firefox download popup

Handle Firefox Download Popup

  1. Firefox version 45.01
  2. Set Firefox browser.helperApps.neverAsk.saveToDisk preference’s value to the download file mime type (commonly depend on the file’s extension such as application/zip for zip file, application/pdf  for pdf file)
  3. Please see code comments for more detail explanation.
    	public void testFirefoxDownloadPopup() throws InterruptedException {
    		/* Create a new FireFox Profile instance. */
    		FirefoxProfile ffProfile = new FirefoxProfile();
    		/* Set file save to directory. */
    		ffProfile.setPreference("", "C:\\WorkSpace");
    		ffProfile.setPreference("", 2);
    		/* Set file mime type which do not show save to popup dialog.
    		 * If the file format is zip, then we set this
    		 * preference's value to application/zip. If file format is csv then
    		 * you need set the value to application/csv.
    		 *  */
    		ffProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip;"); 
    		/* If download pdf.*/
    		//ffProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf;"); 
    		ffProfile.setPreference( "", false );
    		ffProfile.setPreference( "pdfjs.disabled", true );
    		/* Create Firefox browser based on the profile just created. */
    		FirefoxDriver ffDriver = new FirefoxDriver(ffProfile);  
    		/* For tomcat */
    		/* Wait 10 seconds for the process complete. */
    		System.out.println("Task complete, please go to save folder to see it.");

Handle Show pdf In Chrome Browser

chrome download show pdf in browser

  1. Chrome version is 59.0.3071.115
  2. Configure Chrome options with WebDriver. Below method is used to configure not show pdf directly in browser.
    configure not show pdf in chrome browser

    	/* Change option to not show pdf in browser directly. */
    	private void setChromeOptions(ChromeDriver cDriver)
    		/* Go to Chrome configure options page. */
    		/* Find the pdf configure section input checkbox.*/
    		By pdfSectionBy ="pdf-section");
    		WebElement pdfSectionElement = cDriver.findElement(pdfSectionBy);
    		/* Find checkbox in configure section. */
    		By inputBy = By.tagName("input");
    		WebElement pdfSectionInput = pdfSectionElement.findElement(inputBy);
    		/* If not checked then check it. */
  3. Below method implement download zip or pdf  use Chrome. Please see java code comments for more detail.
    	public void testChromeDownloadPopup() throws InterruptedException {
    		/* Set string variable value to ChromeDriver executable file path.*/
    		String chromeDriverPath = "C:\\Workspace\\\\Lib\\chromedriver_win32\\chromedriver.exe";
    		/* Assign chromeDriverPath to system property "" */
    		System.setProperty("", chromeDriverPath);
    		Map<String, Object> chromePreferences = new Hashtable<String, Object>();
    		/* Below two chrome preference settings will disable popup dialog when download file.*/
    		chromePreferences.put("profile.default_content_settings.popups", 0);
    		chromePreferences.put("download.prompt_for_download", "false");
    		/* Set file save to directory. */
    		chromePreferences.put("download.default_directory", "C:\\WorkSpace");
    		ChromeOptions chromeOptions = new ChromeOptions();
    		chromeOptions.setExperimentalOption("prefs", chromePreferences);
    		DesiredCapabilities cap =;
    		cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    		cap.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
    		//Initiate ChromeDriver
    		ChromeDriver cDriver = new ChromeDriver(cap);
    		/* For tomcat */
    		/* For pdf. 
    		 * First check not show pdf in Chrome browser.
    		 * */
    		System.out.println("Task complete, please go to save folder to see it.");

[download id=”1464″]

0 0 vote
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Oldest Most Voted
Inline Feedbacks
View all comments

The point 2. for chrome is a great idea, but not necessary. You can find the name of preference checkbox by Inspect menu:

( input pref=”plugins.always_open_pdf_externally” type=”checkbox” )

and configure it together with other preferences in point 3. by adding line:

chromePreferences.put(“plugins.always_open_pdf_externally”, “true”)

Reply to  Hynek

Thank you:)

Would love your thoughts, please comment.x