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


69

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

线程“主”中的异常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过。但还是一样


1
我认为这不是重复的问题,它是随selenium 3一个新问题而发生的,stacktrace也不同。谢谢.. :)
Saurabh Gaur

1
您需要geckodriver在Firefox中启动硒3测试
Ripon Al Wasim

Answers:


94

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

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

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

使用任何编程语言绑定来启动最新的firefox的以下所有配置都适用于Selenium2显式启用Marionette。在Selenium 3.0及更高版本中,您不需要做任何事情来使用木偶,因为它是默认启用的。

要在测试中使用木偶,您将需要更新所需的功能才能使用它。

Java的

作为例外,很明显,您需要geckodriver.exe此处下载最新版本,并geckodriver.exe使用变量将系统中存在的已下载路径设置为具有变量的系统属性,webdriver.gecko.driver然后再启动木偶驱动程序并启动Firefox,如下所示:-

//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任何语言使用,这将允许您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)

红宝石

# 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。


2
非常感谢。这很完美。但是,仅作为参考,这只壁虎驱动程序的全部作用是什么?我从事硒工作已经有一段时间了,之前我从未遇到过。但是现在我刚刚更换了机器,却出现了这个错误。
Reema

1
与其他浏览器供应商提供给Selenium的其他驱动程序一样,Mozilla现在发布了一个可执行文件,它将与浏览器一起运行。有alook了解更多详情.. developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/...
SAURABH野牛

2
我决定找到另一个很少进行此类重大更改的库。严重的是,在我更新了Firefox或库之后,通常它将不再起作用。更糟糕的是,最新geckodriver.exe的Windows仅适用于64位系统。是时候跟硒说再见了。
Thariq Nugrohotomo '16

1
我真的很困惑 “ Selenium客户端绑定将尝试找到geckodriver” WebDriver是否变成Geckdriver?这里发生了什么?
8protons

1
@ 8protons在这里看看developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/…Mozilla提供的geckodriver,.. geckodriver实现了WebDriver,因此,硒客户端绑定试图找到geckodriver以支持最新的firefox上的自动化
Saurabh Gaur

21
  1. 从seleniumhq网站下载gecko驱动程序(现在它在GitHub上,您可以从Here下载 )。
    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();

我错过了此驱动程序的路径,现在
却无法

3

Selenium WebDriver Java代码:

根据您的平台从https://github.com/mozilla/geckodriver/releases下载Gecko驱动程序 。根据您的选择将其提取。编写以下代码:

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

位置可能因人而异。我将其提取到D驱动器上
Ripon Al Wasim,

0

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

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

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

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

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

0

就我而言,我必须在属性文件中设置路径,在许多小时中,我找到了方法:

application.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();
    }

}

0

我在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();
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.