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

我正在尝试启动 Mozilla,但仍然得到这个错误:

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

我使用的是 Selenium 3.0.01测试版和 Mozilla 45。我也试过 Mozilla 47。但还是一样的东西。

203236 次浏览

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

  • Unix系统上,如果您使用的是与 bash 兼容的 shell,那么您可以执行以下操作来将它追加到系统的搜索路径中:

    export PATH=$PATH:/path/to/geckodriver
    
  • On Windows you need to update the Path system variable to add the full directory path to the executable. The principle is the same as on Unix.

All below configuration for launching latest firefox using any programming language binding is applicable for Selenium2 to enable Marionette explicitly. With Selenium 3.0 and later, you shouldn't need to do anything to use Marionette, as it's enabled by default.

To use Marionette in your tests you will need to update your desired capabilities to use it.

Java :

As exception is clearly saying you need to download latest geckodriver.exe from here and set downloaded geckodriver.exe path where it's exists in your computer as system property with with variable webdriver.gecko.driver before initiating marionette driver and launching firefox as below :-

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

鲁比 :

# 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网格中使用 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)

鲁比 :

# 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 现在发布了一个可执行文件,它将与浏览器一起运行。请按照 这个了解更多细节。

你可以从这里下载最新的 geckoDriver 可执行文件来支持最新的火狐

  1. 从 seleniumhq 网站下载 gecko 驱动程序(现在它在 GitHub 上,可以从 给你下载)。
    1. 您将获得一个 zip (或 tar.gz) ,因此将其解压缩。
    2. 提取之后,您将拥有 geckodriver.exe 文件(Linux 中的适当可执行文件)。
    3. 用 C 语言创建文件夹: 命名为 SeleniumGecko (或者适当)
    4. 复制粘贴 geckodriver.exe 到 SeleniumGecko
    5. 如下所示设置壁虎驾驶员的路径

.

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 Driver。根据您的选择将其提取到一个位置。编写以下代码:

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 中的每个 Driver 服务在创建驱动程序对象时调用类似的代码(下面是特定于 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”,这可以在创建驱动程序对象之前设置,如下所示

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

在我的情况下,我必须在属性文件中设置路径,在许多小时内我找到了方法:

Properties 文件:

webdriver.gecko.driver="/lib/geckodriver-v0.26.0-win64/geckodriver.exe"

在 java 代码中:

private static final Logger log = Logger.getLogger(Login.class.getName());
private FirefoxDriver driver;
private FirefoxProfile firefoxProfile;
private final String BASE_URL = "https://www.myweb.com/";
private static final String RESOURCE_NAME = "main/resources/application.properties"; // could also be a constant
private Properties properties;


public Login() {
init();
}


private void init() {
properties = new Properties();
try(InputStream resourceStream = getClass().getClassLoader().getResourceAsStream(RESOURCE_NAME)) {
properties.load(resourceStream);
} catch (IOException e) {
System.err.println("Could not open Config file");
log.log(Level.SEVERE, "Could not open Config file", e);
}
// open incognito tab by default
firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.privatebrowsing.autostart", true);
// geckodriver driver path to run
String gekoDriverPath = properties.getProperty("webdriver.gecko.driver");
log.log(Level.INFO, gekoDriverPath);
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + gekoDriverPath);
log.log(Level.INFO, System.getProperty("webdriver.gecko.driver"));
System.setProperty("webdriver.gecko.driver", System.getProperty("webdriver.gecko.driver").replace("\"", ""));
if (driver == null) {
driver = new FirefoxDriver();
}


}

我在 windows 10中使用了 selenium-java-3.141.59,并用这段代码解决了我的问题:

System.setProperty("webdriver.gecko.driver", "C:\\gecko\\geckodriver.exe");
System.setProperty("webdriver.firefox.bin","C:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();