Home > Mobile >  How to start driver with path, service and options
How to start driver with path, service and options

Time:07-14

I work with driver´s for Chrome, Firefox and Edge in my Application and have always the same problem. So for this post i reduce my request to the Chrome driver as an example.

What is needed:

  1. Path to the chromedriver.exe
  2. Hide the console
  3. Change of the default Download directory of the browser

Here is a Page that show us the Chrome Driver Class and its options: https://www.selenium.dev/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Chrome_ChromeDriver.htm

But my 3 needed Points are not listed together.

Here is a snipped of my Code with Commends:

string str_DriverPath = @"C:\_MT5_TOOLS\DRIVER\CHROME";

// hide Console
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;

//// change Standard-Download-Path
ChromeOptions options = new ChromeOptions();
var downloadDirectory = GlobalVars.RootPath   @"Pool\"   GlobalVars.strSymbol   @"\"   GlobalVars.strSymbol   @"_"   GlobalVars.strPeriod;
options.AddUserProfilePreference("download.default_directory", downloadDirectory);
options.AddUserProfilePreference("download.prompt_for_download", false);
options.AddUserProfilePreference("disable-popup-blocking", "true");

// Start Driver:
//webdriver = new ChromeDriver(service, options);        // works fine
//webdriver = new ChromeDriver(str_DriverPath, options); // works fine

webdriver = new ChromeDriver(str_DriverPath, service, options); // will not work

How to combine my 3 Points into one driver?

CodePudding user response:

After trying every possible thing around the environment variable "PATH" to set the driver path, it seems to be impossible in 2022. From manually until coded this still not work for me! Maybe this was a trick around in the past...

Solution:

On another portal I found a really nice and easy working solution! The second Line in the following Code does the Trick. It is just the service, where the PATH to the driver can be placed in. This works identical for Edge, Chrome, and Firefox.

// DriverService with Path to driver.exe
ChromeDriverService service = ChromeDriverService.CreateDefaultService(@"C:\_MT5_TOOLS\DRIVER\CHROME");
// hide driver Console? true/false 
service.HideCommandPromptWindow = true;

// change Standard-Download-Path
ChromeOptions options = new ChromeOptions();
var downloadDirectory = GlobalVars.RootPath   @"Pool\"   GlobalVars.strSymbol   @"\"   GlobalVars.strSymbol   @"_"   GlobalVars.strPeriod;
options.AddUserProfilePreference("download.default_directory", downloadDirectory);
options.AddUserProfilePreference("download.prompt_for_download", false);
options.AddUserProfilePreference("disable-popup-blocking", "true");

// Selenium Driver starten:
webdriver = new ChromeDriver(service, options);
  • Related