c# - 在 Selenium 的 FirefoxOptions 中设置 BrowserExecutableLocation 并不能防止 "Unable to find a matching set of capabilities"错误

标签 c# selenium firefox selenium-webdriver selenium-firefoxdriver

我对 Selenium 还很陌生,正在尝试创建一些最低限度通过的测试用例(我想你可以将它们称为某种意义上的“hello world”程序)。

我尝试创建一个 Firefox 驱动程序的实例,如下所示:

var options = new FirefoxOptions()
{
    BrowserExecutableLocation = @"C:\Program Files(x86)\Mozilla Firefox\Firefox.exe",
    Profile = new FirefoxProfile(),
    LogLevel = FirefoxDriverLogLevel.Debug
};

firefoxDriver = new FirefoxDriver(options);

但是,当我运行测试时,出现以下错误:无法找到匹配的功能集。我在 Stack Overflow 和其他地方读到的其他几个答案建议解决此问题的方法是显式指定二进制文件的位置,如下所示:

firefoxDriver = new FirefoxDriver(new FirefoxBinary(@"C:\Program Files (x86)\Mozilla Firefox\firefox.exe"), new FirefoxProfile());

当我尝试这样做时,它有效,但我收到以下编译器警告:

警告 CS0618 'FirefoxDriver.FirefoxDriver(FirefoxBinary, FirefoxProfile)' 已过时:'FirefoxDriver 不应使用 FirefoxBinary 对象构造。请改用 FirefoxOptions。此构造函数将在未来版本中删除。'

既然我在 FirefoxOptions 中明确指定了 BrowserExecutableLocation,那么如果第二个版本有效,为什么第一个版本不能正常工作?有没有办法让我尝试的第一种方法类似,以避免使用第二个已弃用的构造函数?

FWIW,我使用的是 Firefox 52.2.0,我的 NuGet 包设置如下:

<packages>
  <package id="Selenium.Firefox.WebDriver" version="0.18.0" targetFramework="net452" />
  <package id="Selenium.WebDriver" version="3.4.0" targetFramework="net452" />
  <package id="Selenium.WebDriver.IEDriver" version="3.4.0" targetFramework="net452" />
</packages>

最佳答案

如果您特别尝试使用 FirefoxOptions,请尝试以下构造函数:

FirefoxDriver(FirefoxDriverService service, FirefoxOptions options, TimeSpan commandTimeout);

对我来说以下方法不起作用:

FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(Path to Gecko);
service.FirefoxBinaryPath = @"C:\Program Files\Mozilla Firefox\firefox.exe";
driver = new FirefoxDriver(service);

但是以下方法效果很好:

FirefoxDriverService service = FirefoxDriverService.CreateDefaultService("Gecko Path");
FirefoxOptions options = new FirefoxOptions();
options.BrowserExecutableLocation = @"C:\Program Files\Mozilla Firefox\firefox.exe";
driver = new FirefoxDriver(service, options, TimeSpan.FromMinutes(1));

关于c# - 在 Selenium 的 FirefoxOptions 中设置 BrowserExecutableLocation 并不能防止 "Unable to find a matching set of capabilities"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45241596/

相关文章:

c# - 此 RPC 请求中提供的参数过多。最大值为 2100。

JavaScript 函数限制文本区域中的字符,导致 Firefox 中出现 "refresh"问题

c# - 在 VS2010 中向后移植 .NET 4 到 3.5 SP1

c# - 使用 DataContractSerializer 序列化和反序列化多个对象

c# - 如何在 UWP 中创建对话气泡?

java - 如何在页面工厂模型中使用需要 'driver' 参数的函数/方法

selenium - 如何在Selenium远程WebDriver中禁用Flash

google-chrome - 得到 "you are using unsupported command-line flag: --disable-web-security. Stability and security will suffer"错误

firefox - 在 Linux 上的 Jenkins 下运行的 WebDriver 测试中出现 NotConnectedException 错误

javascript - keypress() 是否仅在 Firefox 中聚焦文本字段时触发?