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.
    	//@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.");
    	}

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. */
    		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();
    		}
    	}
  3. 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.");
    	}

Download “Test-Handle-Download-Popup-Code-Example.zip” Test-Handle-Download-Popup-Code-Example.zip – Downloaded 148 times – 2 KB

(Visited 5,351 times, 2 visits today)
READ :   How to Correctly Use GeckoDriver To Run Webdriver Automation Test Script

2 Comments


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

    Reply

Leave a Reply

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.