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

时间:2016-07-30 17:38:13

标签: java selenium firefox

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

  

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

下载

我使用的是Selenium 3.0.01 Beta版和Mozilla 45。我也试过了Mozilla 47。但仍然是一样的。

6 个答案:

答案 0 :(得分:87)

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

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

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

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

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

Java

由于例外明确表示您需要从here下载最新的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();

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,这样您就可以在Marionette网格中使用Selenium

<强>的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

答案 1 :(得分:19)

  1. 从seleniumhq网站下载gecko驱动程序(现在它在GitHub上,你可以从Here下载)。
    1. 你将有一个zip(或tar.gz),所以提取它。
    2. 提取后你会得到geckodriver.exe文件(linux中适当的可执行文件)。
    3. 在C中创建文件夹:名为SeleniumGecko(或适当的)
    4. 将geckodriver.exe复制并粘贴到SeleniumGecko
    5. 设置gecko驱动程序的路径如下
  2. System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.10.0-win64\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    

答案 2 :(得分:2)

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

答案 3 :(得分:0)

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

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

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

for firefox GECKO_DRIVER_EXE_PROPERTY =&#34; webdriver.gecko.driver&#34;这可以在创建驱动程序对象之前设置,如下所示

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

答案 4 :(得分: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();
    }

}

答案 5 :(得分: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();