Setting BrowserExecutableLocation in FirefoxOption

2020-04-14 08:35发布

问题:

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