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

Very Usefull

i am seeing the download dialog in firefox for the above code

I want to download a file using slenium webdriver in Linux Platform.

The above Firefoxprofile code is not working.

Kindly help me on this

Thanks for the great article. Do you know the process for chrome browser ? If know please let me know ... Thanks in Advance

Is it possible to open files from local machine using selenium webdriver, like application is been launched in browser?
Can anyone help on this.

Can you please have the steps on how to do it on chrome?

I am getting nosuchmethod error for above code with firefox 71 version

Add new comment

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