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:
- Use the
ChromeOptions
class. This is supported by Java, Python, etc. - 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
A software Tester who is paid to judge products developed by others. Writing technical posts and creating YouTube videos are my hobbies.
Hi,
I am able to change the default download directory. Is it possible to change the directory dynamically during run time for the existing/running ChromeDriver
prefsMap.put(“download.default_directory”, downloadFilepath);
ChromeOptions option = new ChromeOptions();
option.setExperimentalOption(“prefs”, prefsMap);
The above lines will work only If I call this line
driver = new ChromeDriver(option);
But this line will initiate a new browser, But I need to change the directory path for the existing browse itself
Please Help!
Thanks
Thanks for this wonderful post!!
On running this program I am able to download the file to desired location. And on again running this program, a new file is downloaded on the same location. So, suppose, default.csv got downloaded first and default.csv(1) got downloaded second time.
Is there anything we could do so that on running the code second time, first file gets replaced?
You saved my day!
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
I am too facing the same issues with Windows 10. Do post the solution if you’ve to it.
Thank you
First we have to set prefrence and capability to driver .i followed above code and that works for my frame work
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!
Do you have a post where you show how to download a PDF to this download directory?
I don’t have specific posts but it will work for any file. In fact it a typical download directory z
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!
Thanks Ted.
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
Hi Manohar,
I never tried on Windows XP. I will look for some solution.
Nice article. Waiting for the firefox one. Usually , everyone just post on chrome only.
Thanks Deepak. Firefox post is already published.