3

When I try to use

var dc = DesiredCapabilities.Chrome();
var driver = new ChromeDriver(dc);

I get "Cannot resolve constructor".

It seems like I have to pass ChromeOptions instead.

Why?

Every single tutorial/help page on the subject suggests that I pass DesiredCapabilities.

I am using Selenium.WebDriver.ChromeDriver version 2.21.0.0.

Anders Lindén
  • 6,839
  • 11
  • 56
  • 109

2 Answers2

4

You can use ChromeOptions to set any specific options.

ChromeOptions options = new ChromeOptions();
options.AddArguments("--disable-extensions");
options.AddArguments("--start-maximized");
options.ToCapabilities();
ChromeDriverService service = ChromeDriverService.CreateDefaultService(Environment.GetEnvironmentVariable("USERPROFILE") + "\\Downloads");
IWebDriver chromeDriver = new ChromeDriver(service, options);

You can use- options.ToCapabilities(); to get see the capabilities.

You can use ChromeOptions to set any specific type of capabilities- peter.sh/experiments/chromium-command-line-switches . It seems the DesiredCapabilities can be added only in Java or if you are dealing with InternetExplorerDriver- Selenium c#: How to launch Internet Explorer driver in a specific version (IE8 for example)

Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78
  • Then I will not be able to set them? – Anders Lindén Jun 27 '16 at 09:23
  • You can use ChromeOptions to set any specific type of capabilities- http://peter.sh/experiments/chromium-command-line-switches/ . It seems the DesiredCapabilities can be added only in Java or if you are dealing with InternetExplorerDriver- http://stackoverflow.com/questions/16718608/selenium-c-how-to-launch-internet-explorer-driver-in-a-specific-version-ie8-f . – Souvik Ghosh Jun 27 '16 at 10:29
1

Using dotpeek and looking at the chromedriver constructors (which there are 7 overloads) 6 of them invoke the constructor below on the ChromeDriver itself

public ChromeDriver(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
  : base((ICommandExecutor) new DriverServiceCommandExecutor((DriverService) service, commandTimeout), ChromeDriver.ConvertOptionsToCapabilities(options))
{
}

Which in turn calls the base constructor on the RemoteWebdriver. This passes in the last parameter as ChromeDriver.ConvertOptionsToCapabilities(options)

Looking at the you can see this:

 private static ICapabilities ConvertOptionsToCapabilities(ChromeOptions options)
    {
      if (options == null)
        throw new ArgumentNullException("options", "options must not be null");
      return options.ToCapabilities();
    }

Then into options.ToCapabilities:

public override ICapabilities ToCapabilities()
{
  Dictionary<string, object> dictionary = this.BuildChromeOptionsDictionary();
  DesiredCapabilities desiredCapabilities = DesiredCapabilities.Chrome();
  desiredCapabilities.SetCapability(ChromeOptions.Capability, (object) dictionary);
  if (this.proxy != null)
    desiredCapabilities.SetCapability(CapabilityType.Proxy, (object) this.proxy);
  Dictionary<string, object> preferencesDictionary = this.GenerateLoggingPreferencesDictionary();
  if (preferencesDictionary != null)
    desiredCapabilities.SetCapability(CapabilityType.LoggingPreferences, (object) preferencesDictionary);
  foreach (KeyValuePair<string, object> additionalCapability in this.additionalCapabilities)
    desiredCapabilities.SetCapability(additionalCapability.Key, additionalCapability.Value);

You can see under the hood it appears its already using DesiredCapabilities.Chrome() and you don't need to pass it in. Perhaps the tutorials you have seen are outdated?

Macilquham
  • 282
  • 1
  • 10
  • I seem to have only 5 overloads: ChromeDriver(), ChromeDriver(ChromeDriverService), ChromeDriver(ChromeDriverService, ChromeOptions), ChromeDriver(ChromeDriverService, ChromeOptions, TimeSpan) and ChromeDriver(ChromeOptions) – Anders Lindén Jun 27 '16 at 09:19
  • Perhaps we have different versions I suspect the other points still stand. But you could use a decompiler of your choice to see if the behaviour is still the same e.g. the overloads all refer to one of the constructors which in turn calls the base constructor using ChromeDriver.ConvertOptionsToCapabilities(options) – Macilquham Jun 27 '16 at 09:22