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

 

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

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

    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.
%d bloggers like this: