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

[java]import java.io.File; import java.io.IOException; import java.util.HashMap;

import java.util.Map;