Download file using selenium webdriver

As we know, we cannot simulate OS actions with Selenium. We use AutoIt tool to upload documents (when it is not possible to achive upload using sendKeys method). We have discussed uploading a file using using Webdriver Sendkeys method and Using AutoIT Tool in earlier tutorials.

To handle Downloads with selenium, we need to define settings to the browser using Firefox profile preferences, so that it automatically downloads the files to the specified folder. Then we can write code to check if the file is downloaded or not.

If you want to download and save it to the desired location using Selenium Webdriver, then we need to set below Firefox profile preferences -

profile.setPreference("browser.download.dir", downloadPath);

Below is the example program to download a file

package com.pack;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.Test;

public class FileDownloadExample {
	
	public static String downloadPath = "D:\\seleniumdownloads";
	@Test
	public void testDownload() throws Exception {
		WebDriver driver = new FirefoxDriver(FirefoxDriverProfile());	
		driver.manage().window().maximize();
	    driver.get("http://spreadsheetpage.com/index.php/file/C35/P10/");
	    driver.findElement(By.linkText("smilechart.xls")).click();
	}
	
	public static FirefoxProfile FirefoxDriverProfile() throws Exception {
		FirefoxProfile profile = new FirefoxProfile();
		profile.setPreference("browser.download.folderList", 2);
		profile.setPreference("browser.download.manager.showWhenStarting", false);
		profile.setPreference("browser.download.dir", downloadPath);
		profile.setPreference("browser.helperApps.neverAsk.openFile",
				"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
		profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
		profile.setPreference("browser.helperApps.alwaysAsk.force", false);
		profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
		profile.setPreference("browser.download.manager.focusWhenStarting", false);
		profile.setPreference("browser.download.manager.useWindow", false);
		profile.setPreference("browser.download.manager.showAlertOnComplete", false);
		profile.setPreference("browser.download.manager.closeWhenDone", false);
		return profile;
	}
}

We will explain you the preferences that we have set to Firefox browser.

setPreference("browser.download.folderList", 2);
Default Value: 1
The value of browser.download.folderList can be set to either 0, 1, or 2. When set to 0, Firefox will save all files downloaded via the browser on the user's desktop. When set to 1, these downloads are stored in the Downloads folder. When set to 2, the location specified for the most recent download is utilized again.

setPreference("browser.download.manager.showWhenStarting", false);
Default Value: true
The browser.download.manager.showWhenStarting Preference in Firefox's about:config interface allows the user to specify whether or not the Download Manager window is displayed when a file download is initiated.

browser. helperApps. alwaysAsk. force
True: Always ask what to do with an unknown MIME type, and disable option to remember what to open it with False (default): Opposite of above

browser. helperApps. neverAsk. saveToDisk
A comma-separated list of MIME types to save to disk without asking what to use to open the file. Default value is an empty string.

browser. helperApps. neverAsk. openFile
A comma-separated list of MIME types to open directly without asking for confirmation. Default value is an empty string.

browser. download. dir
The last directory used for saving a file from the "What should (browser) do with this file?" dialog.

browser.download.manager.alertOnEXEOpen
True (default): warn the user attempting to open an executable from the Download Manager
False: display no warning and allow executable to be run
Note: In Firefox, this can be changed by checking the "Don't ask me this again" box when you encounter the alert.

browser. download. manager. closeWhenDone
True: Close the Download Manager when all downloads are complete
False (default): Opposite of the above

browser. download. manager. focusWhenStarting
True: Set the Download Manager window as active when starting a download
False (default): Leave the window in the background when starting a download

Note that you cannot implement this with Internet Explorer, as they don't use profiles. It's a limitation of the browser itself, not the IE driver. As such, there is no way to automatically download files to a specified location with Internet Explorer / Edge Browser.

Hope this helps you.

Selenium Tutorials: 

Comments

This is my code. This works but , the file doesn't gets downloaded in the directory location which i am specifying.

Updated the code. Please check.

If the application is generated dynamically (mime-types) using Chrome browser will be a better approach since the Chrome will not open the file download pop-up.But multiple download option should be enabled if you need multiple downloads.

Hi,

How to achieve download functionality in Chrome and IE?

People are suggesting to use Autoit or Sikuli. Dont we have any smart solution?

Thanks
Siva

When set its value as true in above code then I receive error message:
I want to see download window so I modified the code to profile.setPreference("browser.download.manager.showWhenStarting", true);

rest everything remains same. I see an error message "java.lang.IllegalArgumentException: Preference browser.download.manager.showWhenStarting may not be overridden: frozen value=false, requested value=true" please suggest?

Please suggest similar code for Chrome and IE.

It is good tutorial. If IE and Chrome preferences were told it would be great.

Perfect setup and description. Thanks team.

Can u please write an article explaining how to download file In IE V11 browser using selenium webdriver

Thanks @Seleniumeasy above article is really helped me out to solve my issue with dialog box.
Thanks for such good article.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.