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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38676719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:31:07  来源:igfitidea点击:

Selenium using Java - The path to the driver executable must be set by the webdriver.gecko.driver system property

javaseleniumfirefox

提问by Reema

I am trying to launch Mozilla but still I am getting this error:

我正在尝试启动 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

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

I am using Selenium 3.0.01Beta version and Mozilla 45. I have tried with Mozilla 47too. but still the same thing.

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

采纳答案by Saurabh Gaur

The Seleniumclient bindings will try to locate the geckodriverexecutable from the system PATH. You will need to add the directory containing the executable to the system path.

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

  • On Unixsystems you can do the following to append it to your system's search path, if you're using a bash-compatible shell:

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

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

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

All below configuration for launching latest firefox using any programming language binding is applicable for Selenium2to 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.

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

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

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

Java:

爪哇

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

例外情况很明显,您需要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); 

And for Selenium3use as :-

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:

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

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

Using RemoteWebDriver

使用 RemoteWebDriver

If you want to use RemoteWebDriverin any language, this will allow you to use Marionettein SeleniumGrid.

如果您想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)

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

Note : Just like the other drivers available to Selenium from other browser vendors, Mozilla has released now an executable that will run alongside the browser. Follow thisfor more details.

注意:就像其他浏览器供应商提供给 Selenium 的其他驱动程序一样,Mozilla 现在发布了一个可以与浏览器一起运行的可执行文件。按照了解更多详细信息。

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

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

回答by Kanth25

  1. Download gecko driver from the seleniumhq website (Now it is on GitHub and you can download it from Here) .
    1. You will have a zip (or tar.gz) so extract it.
    2. After extraction you will have geckodriver.exe file (appropriate executable in linux).
    3. Create Folder in C: named SeleniumGecko (Or appropriate)
    4. Copy and Paste geckodriver.exe to SeleniumGecko
    5. Set the path for gecko driver as below
  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();

回答by Ripon Al Wasim

Selenium WebDriver Java code:

Selenium WebDriver Java 代码:

Download Gecko Driver from https://github.com/mozilla/geckodriver/releasesbased on your platform. Extract it in a location by your choice. Write the following code:

根据您的平台从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");

回答by chuha.billi

Every Driver service in selenium calls the similar code(following is the firefox specific code) while creating the driver object

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

now for the driver that you want to use, you have to set the system property with the value of path to the driver executable.

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

for firefox GECKO_DRIVER_EXE_PROPERTY = "webdriver.gecko.driver" and this can be set before creating the driver object as below

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

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

回答by nobjta_9x_tq

in my case, I must to set path in properties file, in many hours I find the way:

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

application.properties file:

application.properties 文件:

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

in java code:

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

}