无法将DesiredCapabilities传递给ChromeDriver:s构造函数?

时间:2016-06-27 08:43:32

标签: c# selenium-webdriver selenium-chromedriver

当我尝试使用

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

我得到"无法解析构造函数"。

似乎我必须改为通过ChromeOptions

为什么?

关于该主题的每个教程/帮助页面都表明我通过了DesiredCapabilities

我使用的是Selenium.WebDriver.ChromeDriver版本2.21.0.0。

2 个答案:

答案 0 :(得分:3)

您可以使用ChromeOptions设置任何特定选项。

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);

您可以使用 - options.ToCapabilities();来查看功能。

您可以使用ChromeOptions设置任何特定类型的功能 - peter.sh/experiments/chromium-command-line-switches。似乎DesiredCapabilities只能在Java中添加,或者如果您正在处理InternetExplorerDriver- Selenium c#: How to launch Internet Explorer driver in a specific version (IE8 for example)

答案 1 :(得分:1)

使用dotpeek并查看chromedriver构造函数(有7个重载),其中6个在ChromeDriver本身上调用下面的构造函数

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

它反过来调用RemoteWebdriver上的基础构造函数。这将最后一个参数传递为ChromeDriver.ConvertOptionsToCapabilities(选项)

看着你可以看到:

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

然后进入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);

你可以看到它已经使用了DesiredCapabilities.Chrome()并且你不需要传入它。或许你看过的教程已经过时了?