Make Selenium Easy

How To Change Default Download Directory For Firefox Browser in Selenium WebDriver

Hello Folks,

Every browse has its default download directory. Whenever you download a file, it gets downloaded in default download directory. Generally default download directory is as below:

C:\Users\<UserName>\Downloads

You can always change it through browser setting.

When selenium script downloads any file, it will also be downloaded in same default download directory.

Note: If you change default download directory manually through browser setting, it will not be same when selenium scripts launch a browser. Selenium launch browser with default settings.

It is advisable to download files through automation script in a separate folder where you can easily verify successful download of file. It also helps you to keep all related downloaded file at one place for a project. I generally keep downloaded files with in project hierarchy.

Setting custom download directory is not straightforward as Chrome browser. It has three steps to do:

  1. Let Firefox know that you want to change default download directory to user defined.

      profile.setPreference(“browser.download.folderList”, 2);

2. Set the user defined download directory path

      profile.setPreference(“browser.download.dir”, System.getProperty(“user.dir”) + File.separator + “externalFiles”
     + File.separator + “downloadFiles” + File.separator);

3. Surpass Save To Disk popup for file types using theri MIME code separated by comma

      profile.setPreference(“browser.helperApps.neverAsk.saveToDisk”,“application/vnd.microsoft.portable-executable”);

Java Code:

import java.io.File;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;

public class ChangeDownloadDirOfFirefox {

	public static void main(String[] args) throws IOException {

		// Setting Firefox driver path
		System.setProperty("webdriver.gecko.driver", "./exefiles/geckodriver.exe");

		// Creating firefox profile
		FirefoxProfile profile = new FirefoxProfile();

		// Instructing firefox to use custom download location
		profile.setPreference("browser.download.folderList", 2);

		// Setting custom download directory
		profile.setPreference("browser.download.dir", System.getProperty("user.dir") + File.separator + "externalFiles"
				+ File.separator + "downloadFiles" + File.separator);

		// Skipping Save As dialog box for types of files with their MIME
		profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
				"text/csv,application/java-archive, application/x-msexcel,application/excel,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml,application/vnd.microsoft.portable-executable");

		// Creating FirefoxOptions to set profile
		FirefoxOptions option = new FirefoxOptions();
		option.setProfile(profile);
		// Launching browser with desired capabilities
		FirefoxDriver driver = new FirefoxDriver(option);

		// URL loading
		driver.get("https://www.seleniumhq.org/download/");

		// Click on download selenium server jar file
		driver.findElement(By.xpath("//p[text()='Download version ']/a")).click();

	}
}

Output:

 

That’s it. Hopefully It will be helpful for you.

#ThanksForReading

#HappySelenium

 

Table of Contents

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

3 thoughts on “How To Change Default Download Directory For Firefox Browser in Selenium WebDriver

  1. where can I get default Download Directory path For Edge Browser In Selenium WebDriver ?

  2. Thanks for information, but i’m trying to test angular website by using selenium with protractor, but i’m facing lot of problem. so please help me

    1. Hi, There is nothing like selenium with protractor. Selenium and protractor both are different tools and protractor is build on selenium. What issues you are facing?

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.