在 Selenium 的 FirefoxOptions 中设置 BrowserExecutableLocation 不会阻止 "Unable to find a matching set of capabilities" 错误

Setting BrowserExecutableLocation in FirefoxOptions in Selenium doesn't prevent an "Unable to find a matching set of capabilities" error

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

然而,当我运行测试时,我得到以下错误:Unable to find a matching set of capabilities。我在 Stack Overflow 和其他地方读到的其他几个答案建议解决这个问题的方法是明确指定二进制文件的位置,如下所示:

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

当我尝试这样做时,它起作用了,但我收到以下编译器警告:

Warning CS0618 'FirefoxDriver.FirefoxDriver(FirefoxBinary, FirefoxProfile)' is obsolete: 'FirefoxDriver should not be constructed with a FirefoxBinary object. Use FirefoxOptions instead. This constructor will be removed in a future release.'

如果第二个版本有效,为什么第一个版本不能正常工作,因为我在 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));