使用Java的Selenium - 必须通过webdriver.gecko.driver系统属性设置驱动程序可执行文件的路径

Ree*_*ema 65 java firefox selenium

我试图启动Mozilla但我仍然收到此错误:

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

我正在使用Selenium 3.0.01Beta版本Mozilla 45.我也试过Mozilla 47了.但仍然是一样的.

Sau*_*aur 90

Selenium客户端绑定将尝试找到geckodriver从系统中可执行文件PATH.您需要将包含可执行文件的目录添加到系统路径.

  • Unix系统上,如果您使用的是与bash兼容的shell,则可以执行以下操作将其附加到系统的搜索路径:

    export PATH=$PATH:/path/to/geckodriver
    
    Run Code Online (Sandbox Code Playgroud)
  • Windows上,您需要更新Path系统变量以添加可执行文件的完整目录路径.原理与Unix相同.

以下所有使用任何编程语言绑定启动最新firefox的配置适用于Selenium2明确启用Marionette.使用Selenium 3.0及更高版本,您不需要做任何事情来使用Marionette,因为它默认启用.

要在测试中使用Marionette,您需要更新所需的功能才能使用它.

Java:

因为例外显然需要geckodriver.exe这里下载最新版本,并在启动marionette驱动程序并启动firefox之前geckodriver.exe将其存在于计算机中的下载路径设置为带变量的系统属性,webdriver.gecko.driver如下所示: -

//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); 
Run Code Online (Sandbox Code Playgroud)

而对于Selenium3使用如下: -

WebDriver driver = new FirefoxDriver();
Run Code Online (Sandbox Code Playgroud)

如果您仍然遇到问题,请点击此链接,这将有助于您解决问题

.NET:

var driver = new FirefoxDriver(new FirefoxOptions());
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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();
Run Code Online (Sandbox Code Playgroud)

运用 RemoteWebDriver

如果要使用RemoteWebDriver任何语言,这将允许您MarionetteSeleniumGrid中使用.

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)
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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); 
Run Code Online (Sandbox Code Playgroud)

.净

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); 
Run Code Online (Sandbox Code Playgroud)

注意:就像Selenium从其他浏览器供应商那里获得的其他驱动程序一样,Mozilla现在已经发布了一个可以与浏览器一起运行的可执行文件.按照了解更多详情.

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

  • 非常感谢.这非常完美.但仅仅是为了获取信息,这个geckodriver到底是什么?我已经使用硒很长一段时间了,之前我没有遇到过这种情况.但是现在我刚刚更换了我的机器并得到了这个错误. (2认同)
  • 我决定找另一个很少像这样进行重大改变的图书馆.说真的,在我更新了firefox或库后,通常它将不再起作用.更糟糕的是,最新的`geckodriver.exe` for windows仅适用于64位系统.现在是时候向Selenium说再见了. (2认同)

小智 19

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

.

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