使用Java的Selenium – 驱动程序可执行文件的path必须由webdriver.gecko.driver系统属性设置

我正在尝试启动Mozilla,但仍然收到此错误:

线程“main”中的exceptionjava.lang.IllegalStateException:驱动程序可执行文件的path必须由webdriver.gecko.driver系统属性设置; 有关更多信息,请参阅https://github.com/mozilla/geckodriver 。 最新版本可以从https://github.com/mozilla/geckodriver/releases下载

我正在使用Selenium 3.0.01 Beta版和Mozilla 45 。 我也尝试过使用Mozilla 47 。 但仍然是同样的事情。

Selenium客户端绑定将尝试从系统PATHfindgeckodriver可执行文件。 您将需要将包含可执行文件的目录添加到系统path。

  • Unix系统上,如果您使用的是兼容bash的shell程序,则可以执行以下操作将其附加到系统的searchpath中:

     export PATH=$PATH:/path/to/geckodriver 
  • Windows上,您需要更新Path系统variables以将完整的目录path添加到可执行文件。 原理和Unix上一样。

以下所有使用任何编程语言绑定启动最新Firefox的configuration都适用于Selenium2以明确地启用Marionette。 使用Selenium 3.0和更高版本,您不需要做任何事情就可以使用Marionette,因为它默认是启用的。

要在testing中使用Marionette,您需要更新所需的function以使用它。

Java

作为exception很明显,你需要从这里下载最新的geckodriver.exe ,并在启动木偶驱动程序和启动firefox之前,使用variableswebdriver.gecko.driver将下载的geckodriver.exepath设置为系统属性的系统属性,如下所示:

 //if you didn't update the Path system variable to add the full directory path to the executable as above mentioned then doing this directly through code System.setProperty("webdriver.gecko.driver", "path/to/geckodriver.exe"); //Now you can Initialize marionette driver to launch firefox DesiredCapabilities capabilities = DesiredCapabilities.firefox(); capabilities.setCapability("marionette", true); WebDriver driver = new MarionetteDriver(capabilities); 

对于Selenium3使用:

 WebDriver driver = new FirefoxDriver(); 

如果你仍然遇到麻烦,请按照这个链接,这将有助于你解决你的问题

.NET

 var driver = new FirefoxDriver(new FirefoxOptions()); 

Python

 from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities caps = DesiredCapabilities.FIREFOX # Tell the Python bindings to use Marionette. # This will not be necessary in the future, # when Selenium will auto-detect what remote end # it is talking to. caps["marionette"] = True # Path to Firefox DevEdition or Nightly. # Firefox 47 (stable) is currently not supported, # and may give you a suboptimal experience. # # On Mac OS you must point to the binary executable # inside the application package, such as # /Applications/FirefoxNightly.app/Contents/MacOS/firefox-bin caps["binary"] = "/usr/bin/firefox" driver = webdriver.Firefox(capabilities=caps) 

ruby

 # Selenium 3 uses Marionette by default when firefox is specified # Set Marionette in Selenium 2 by directly passing marionette: true # You might need to specify an alternate path for the desired version of Firefox Selenium::WebDriver::Firefox::Binary.path = "/path/to/firefox" driver = Selenium::WebDriver.for :firefox, marionette: true 

JavaScript(Node.js)

 const webdriver = require('selenium-webdriver'); const Capabilities = require('selenium-webdriver/lib/capabilities').Capabilities; var capabilities = Capabilities.firefox(); // Tell the Node.js bindings to use Marionette. // This will not be necessary in the future, // when Selenium will auto-detect what remote end // it is talking to. capabilities.set('marionette', true); var driver = new webdriver.Builder().withCapabilities(capabilities).build(); 

使用RemoteWebDriver

如果您想使用任何语言的RemoteWebDriver ,这将允许您在Selenium Grid中使用Marionette

Python

 caps = DesiredCapabilities.FIREFOX # Tell the Python bindings to use Marionette. # This will not be necessary in the future, # when Selenium will auto-detect what remote end # it is talking to. caps["marionette"] = True driver = webdriver.Firefox(capabilities=caps) 

ruby

 # Selenium 3 uses Marionette by default when firefox is specified # Set Marionette in Selenium 2 by using the Capabilities class # You might need to specify an alternate path for the desired version of Firefox caps = Selenium::WebDriver::Remote::Capabilities.firefox marionette: true, firefox_binary: "/path/to/firefox" driver = Selenium::WebDriver.for :remote, desired_capabilities: caps 

Java

 DesiredCapabilities capabilities = DesiredCapabilities.firefox(); // Tell the Java bindings to use Marionette. // This will not be necessary in the future, // when Selenium will auto-detect what remote end // it is talking to. capabilities.setCapability("marionette", true); WebDriver driver = new RemoteWebDriver(capabilities); 

。净

 DesiredCapabilities capabilities = DesiredCapabilities.Firefox(); // Tell the .NET bindings to use Marionette. // This will not be necessary in the future, // when Selenium will auto-detect what remote end // it is talking to. capabilities.SetCapability("marionette", true); var driver = new RemoteWebDriver(capabilities); 

注意:就像其他浏览器供应商提供给Selenium的其他驱动程序一样,Mozilla现在已经发布了一个可以与浏览器一起运行的可执行文件。 按照这个更多的细节。

您可以从这里下载最新的geckodriver可执行文件来支持最新的Firefox

  1. 从seleniumhq网站下载gecko驱动(现在在GitHub上,你可以从这里下载)。
    1. 你将有一个zip(或tar.gz)所以提取它。
    2. 解压后你将有geckodriver.exe文件(在linux中适当的可执行文件)。
    3. 在C中创build文件夹:名为SeleniumGecko(或适当的)
    4. 将geckodriver.exe复制并粘贴到SeleniumGecko
    5. 如下所示设置gecko驱动程序的path

 System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.10.0-win64\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); 

Selenium WebDriver Java代码:

https://github.com/mozilla/geckodriver/releases根据您的平台下载Gecko驱动程序。; 在您select的位置提取它。 写下面的代码:

 System.setProperty("webdriver.gecko.driver", "D:/geckodriver-v0.16.1-win64/geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.lynda.com/Selenium-tutorials/Mastering-Selenium-Testing-Tools/521207-2.html"); 

Selenium中的每个驱动程序服务在创build驱动程序对象时调用类似的代码(以下是特定于Firefox的代码)

  @Override protected File findDefaultExecutable() { return findExecutable( "geckodriver", GECKO_DRIVER_EXE_PROPERTY, "https://github.com/mozilla/geckodriver", "https://github.com/mozilla/geckodriver/releases"); } 

现在对于要使用的驱动程序,必须使用驱动程序可执行文件的path值设置系统属性。

对于firefox GECKO_DRIVER_EXE_PROPERTY =“webdriver.gecko.driver”,这可以在创build驱动程序对象之前设置如下

 System.setProperty("webdriver.gecko.driver", "./libs/geckodriver.exe"); WebDriver driver = new FirefoxDriver();