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 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

11 thoughts on “How To Change Default Download Directory For Chrome Browser in Selenium WebDriver

  1. Hi

    It is working fine in macOS but I tried the same code in Windows 10 and it is not working :(, anyone has the same issue?

    Thanks

  2. First we have to set prefrence and capability to driver .i followed above code and that works for my frame work

  3. the challenge i am having is this: the PDF opens in Chrome, then i don’t know how to make the download happen in Selenium. I tried Cntl+S but that opens a Windows dialog which I can’t manage from Selenium. Now i’m thinking to back up a page to the URL that loaded the PDF and try to “Save Link As…”. Have you already solved this puzzle?
    Many thanks again!

  4. Excellent article! You totally helped me in my time of need after I searched all over the web for this answer. Thank you many times!

  5. I am unable to change the download directory in Windows XP with above commands. It is working fine in Windows7. Any other commands for XP?
    Note: Chrome version in XP 49.0.2623.112 m

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: