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

标签 java selenium firefox

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

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases

我正在使用 Selenium 3.0.01 Beta 版和 Mozilla 45。我也尝试过使用 Mozilla 47。但还是一样。

最佳答案

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

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

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

以下所有使用任何编程语言绑定(bind)启动最新版 firefox 的配置都适用于 Selenium2 以显式启用 Marionette。使用 Selenium 3.0 及更高版本,您无需执行任何操作即可使用 Marionette,因为它已默认启用。

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

Java:

因为异常明确表示您需要从 here 下载最新的 geckodriver.exe并将下载的 geckodriver.exe 路径设置为您计算机中存在的系统属性,并在启动 marionette 驱动程序和启动 firefox 之前使用变量 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); 

对于 Selenium3 使用:-

WebDriver driver = new FirefoxDriver();

If you're still in trouble follow this link as well which would help you to solving your problem

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

.NET

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 现在发布了一个可以与浏览器一起运行的可执行文件。关注 this了解更多详情。

You can download latest geckodriver executable to support latest firefox from here

关于java - Selenium 使用 Java - 驱动程序可执行文件的路径必须由 webdriver.gecko.driver 系统属性设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38676719/

相关文章:

java - Chrome 浏览器选项可停止导航到检查点页面以实现注册功能自动化

python - 如何在 Heroku 上的特定目录 (/app/assets/chromedriver) 中安装 chromedriver?

java - 在 Eclipse 中独立运行所有 JUnit 测试,每次都重新加载 Spring 上下文

javascript - 如何使用 selenium webdriver 查找网页/表格中的重复文本/记录?

javascript - SVG - 从窗口坐标到 ViewBox 坐标

javascript - 尝试向 CSS 样式表添加规则在 Firefox 中给出 "The operation is insecure"

javascript - 一键登录

java - 流对象初始化

java - Tomcat 6 默认缓冲区大小

java - 在应用程序级别调整 TCP 数据包大小以实现最大吞吐量