7

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);
GroundIns
  • 541
  • 1
  • 5
  • 11
  • Hello, you might wanna see this question, I know is about python but there is an answer for C#, the first one: http://stackoverflow.com/questions/16328801/define-download-directory-for-chromedriver-selenium-with-python – Ivan Rascon Mar 17 '16 at 14:34
  • 1
    @IvanRascon but it seems like he is **Defining the download path** I just need to get the current default download path – GroundIns Mar 17 '16 at 14:43
  • @GroundIns were you able to get download directory of chrome browser ? – sjethvani Jan 12 '17 at 12:33

3 Answers3

2

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!

Xwris Stoixeia
  • 1,831
  • 21
  • 22
1
driver.get("chrome://settings/?search=Downloads");

Then take a screenshot.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Sai vinay
  • 21
  • 2
0

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.

Brij
  • 11,731
  • 22
  • 78
  • 116