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 Download functionality with selenium, we need to do some settings to the browser using Firefox profile using preferences, so that it automatically download the files to the defined folder. Then we can write code to check if the folder 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("", 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";
	public void testDownload() throws Exception {
		WebDriver driver = new FirefoxDriver(FirefoxDriverProfile());	
	public static FirefoxProfile FirefoxDriverProfile() throws Exception {
		FirefoxProfile profile = new FirefoxProfile();
		profile.setPreference("", 2);
		profile.setPreference("", false);
		profile.setPreference("", downloadPath);
		profile.setPreference("browser.helperApps.alwaysAsk.force", false);
		profile.setPreference("", false);
		profile.setPreference("", false);
		profile.setPreference("", false);
		profile.setPreference("", false);
		profile.setPreference("", false);
		return profile;

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

setPreference("", 2);
Default Value: 1
The value of 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("", false);
Default Value: true
The 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.
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: 


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.


How to achieve download functionality in Chrome and IE?

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


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("", true);

rest everything remains same. I see an error message "java.lang.IllegalArgumentException: Preference 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