get the default downloads folder path using Selenium WebDriver

Viewed 10787

I am new to selenium and I would like to know how do I get the default path of the downloads folder of the browser(I'm using chrome) in the operating system.

I just found a way to set the default path like this:

 var chromeOptions = new ChromeOptions();
 chromeOptions.AddUserProfilePreference("download.default_directory", path);
 chromeOptions.AddUserProfilePreference("intl.accept_languages", "nl");
 chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");
 var driver = new ChromeDriver("Driver_Path", chromeOptions);
3 Answers

2 ways of achieving this.

One can be found here: Find Chrome Path

Seconds is to type on browswer (can do that via sendKeys):

chrome://settings

then instruct your webdriver to click on 'advanced' and finally you can grab the default download directory from the 'location'

If you get stuck anywhere or want further help, please let me know. Best of luck!

driver.get("chrome://settings/?search=Downloads");

Then take a screenshot.

I had similar need to get the download path, I opted for setting it explicitly. If you could explicitly set the path while creating the driver, you can use it.

For my case, I created a new class inheriting from ChromeDriver which holds the downloads path.

public class MyChromeDriver : ChromeDriver
{
    public string DownloadsPath { get; set; }
    
    public MyChromeDriver(ChromeOptions options) : base(options) { }
}

Then when I need a new ChromeDriver, I create new MyChromeDriver and set the downloads path.

var options = new ChromeOptions();
var downloadsPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(downloadsPath);
options.AddUserProfilePreference("download.default_directory", downloadsPath);
options.AddUserProfilePreference("download.prompt_for_download", false);
options.AddUserProfilePreference("disable-popup-blocking", "true");
var myChromeDriver = new MyChromeDriver(options);
myChromeDriver.DownloadsPath = downloadsPath;

Now whenever I need the download path, I have it in myChromeDriver.DownloadPath.

Related