How To Change Default Download Directory For Chrome 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.

In selenium, we can change default download directory in Chrome browser using ChromeOptions class. We can also use DesiredCapabilities class to achieve the same but usage of this class is deprecated since 3.6 release of Selenium.

As per Chromedriver official document:

The WebDriver language APIs provides ways to pass capabilities to ChromeDriver. The exact mechanism differs by the language, but most languages use one or both of the following mechanisms:

  1. Use the ChromeOptions class. This is supported by Java, Python, etc.
  2. Use the DesiredCapabilities class. This is supported by Python, Ruby, etc. While it is also available in Java, its usage in Java is deprecated.

 

We need to add capabilities to chrome browser using key “download.default_directory” and value as desired download location.

Java Code:

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class ChangeDownloadDirOfChrome {

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

		// Setting chrome driver path
		System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");

		// Setting new download directory path
		Map<String, Object> prefs = new HashMap<String, Object>();
		
		// Use File.separator as it will work on any OS
		prefs.put("download.default_directory",
				System.getProperty("user.dir") + File.separator + "externalFiles" + File.separator + "downloadFiles");
		
		// Adding cpabilities to ChromeOptions
		ChromeOptions options = new ChromeOptions();
		options.setExperimentalOption("prefs", prefs);
		
		// Printing set download directory
		System.out.println(options.getExperimentalOption("prefs"));
		
		// Launching browser with desired capabilities
		ChromeDriver driver= new ChromeDriver(options);
		
		// 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();
		

	}
}

When you run above program, you will see file is downloaded in desired directory.

Output:

In next post, we will see how can we change download directory in Firefox browser.

#ThanksForReading
#HappySelenium

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw 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