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

风流意气都作罢 提交于 2019-12-02 05:01:29

问题


I'm still fairly new to Selenium and am trying to create some minimally passing test cases (I guess you could call them the equivalent of a "hello world" program in a sense).

I tried to create an instance of the Firefox Driver like this:

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

firefoxDriver = new FirefoxDriver(options);

However, when I ran the test, I got the following error: Unable to find a matching set of capabilities. Several other answers I read on Stack Overflow and elsewhere suggested that the way to fix this is to explicitly specify the location of the binary file, like this:

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

When I try that, it works, but I get the following compiler warning:

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.'

If the second version works, why doesn't the first version work as well, given that I clearly specified the BrowserExecutableLocation in FirefoxOptions? Is there a way to make something like the first way I tried work in order to avoid using the second, deprecated constructor?

FWIW, I'm using Firefox 52.2.0, and my NuGet packages are set as follows:

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

回答1:


If you are trying to use FirefoxOptions in particular, try this constructor:

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

For me the following did not work:

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

However the following works well:

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


来源:https://stackoverflow.com/questions/45241596/setting-browserexecutablelocation-in-firefoxoptions-in-selenium-doesnt-prevent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!