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 that you can not handle always. When you download a pdf use Chrome browser, you may find that it is opened in the browser tab instead of being downloaded. In this article, we will tell you how to resolve such issues by example.

1. 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 detailed explanations.
    //@Test
    public void testFirefoxDownloadPopup() throws InterruptedException {
    	/* Create a new FireFox Profile instance. */
    	FirefoxProfile ffProfile = new FirefoxProfile();
    		 
    	/* Set file save to directory. */
    	ffProfile.setPreference("browser.download.dir", "C:\\WorkSpace");
    	ffProfile.setPreference("browser.download.folderList", 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( "browser.download.manager.showWhenStarting", false );
    	ffProfile.setPreference( "pdfjs.disabled", true );
    		 
    	/* Create Firefox browser based on the profile just created. */
    	FirefoxDriver ffDriver = new FirefoxDriver(ffProfile);  
    		 
    	/* For tomcat 9.0.zip. */
    	ffDriver.get("http://mirror.nexcess.net/apache/tomcat/tomcat-9/v9.0.0.M22/bin/apache-tomcat-9.0.0.M22.zip");
    		 
    	//ffDriver.get("https://docs.oracle.com/javaee/7/JEETT.pdf");
    		
    	/* Wait 10 seconds for the process complete. */
    	Thread.sleep(10000);
    		
    	System.out.println("Task complete, please go to save folder to see it.");
    }

2. Handle Show pdf In Chrome Browser.

  1. Chrome version is 59.0.3071.115
  2. Configure Chrome options with WebDriver. The below method is used to configure not show pdf directly in the Google Chrome web browser.
  3. Input the URL chrome://settings-frame/content in the Google Chrome browser address input text box.
  4. It will open the Content settings dialog, scroll down to the PDF Documents section.
  5. Check the Open PDF files in the default PDF viewer application checkbox.
  6. Below source code will perform the above action in the selenium webdriver java source code.
    /* Change option to not show pdf in browser directly. */
    private void setChromeOptions(ChromeDriver cDriver)
    {
    	/* Go to Chrome configure options page. */
    	cDriver.get("chrome://settings-frame/content");
    		
    	/* Find the pdf configure section input checkbox.*/
    	By pdfSectionBy = By.id("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. */
    	if(!pdfSectionInput.isSelected())
    	{
    		pdfSectionInput.click();
    	}
    }
  7. Below method implement download zip or pdf use Chrome. Please see java code comments for more detail.
    @Test
    public void testChromeDownloadPopup() throws InterruptedException {
    	
    	/* Set string variable value to ChromeDriver executable file path.*/
    	String chromeDriverPath = "C:\\Workspace\\dev2qa.com\\Lib\\chromedriver_win32\\chromedriver.exe";
    	/* Assign chromeDriverPath to system property "webdriver.chrome.driver" */
    	System.setProperty("webdriver.chrome.driver", 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 = DesiredCapabilities.chrome();
    	cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    	cap.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
    		  
    	//Initiate ChromeDriver
    	ChromeDriver cDriver = new ChromeDriver(cap);
    		
    	/* For tomcat 9.0.zip. */
    	//cDriver.get("http://mirror.nexcess.net/apache/tomcat/tomcat-9/v9.0.0.M22/bin/apache-tomcat-9.0.0.M22.zip");
    		
    		
    	/* For pdf. 
    	 * First check not show pdf in Chrome browser.
    	 * */
    	this.setChromeOptions(cDriver);
    	cDriver.get("https://docs.oracle.com/javaee/7/JEETT.pdf");
    		
    	System.out.println("Task complete, please go to save folder to see it.");
    }

2 thoughts on “Selenium Webdriver Handle Firefox Or Chrome Download Popup”

  1. 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”)

Leave a Comment

Your email address will not be published. Required fields are marked *

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