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:


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(“”, 2);

2. Set the user defined download directory path

      profile.setPreference(“”, 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


Java Code:

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

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

		// Skipping Save As dialog box for types of files with their MIME
				"text/csv,application/java-archive, application/x-msexcel,application/excel,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/x-excel,application/,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml,application/");

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

		// URL loading

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




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




Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning