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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43418035/
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-11-03 07:24:55  来源:igfitidea点击:

The path to the driver executable must be set by the webdriver.gecko.driver system property;

javaseleniumfirefox

提问by Ali Hesari

I am using Selenium 3.3.1and I'm testing the code below.

我正在使用 Selenium 3.3.1,我正在测试下面的代码。

After running the following error is displayed:

运行后显示如下错误:

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/releasesat com.google.common.base.Preconditions.checkState(Preconditions.java:738) at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:111) at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:38) at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:112) at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:302) at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:233) at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:125) at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:121) at Selenium_login.(Selenium_login.java:13) at Selenium_login.main(Selenium_login.java:70) /home/ali/.cache/netbeans/dev/executor-snippets/run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

线程“main”中的异常 java.lang.IllegalStateException:驱动程序可执行文件的路径必须由 webdriver.gecko.driver 系统属性设置;有关更多信息,请参阅 https://github.com/mozilla/geckodriver。最新版本可从https://github.com/mozilla/geckodriver/releases下载在 com.google.common.base.Preconditions.checkState(Preconditions.java:738) 在 org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:111) 在 org.openqa.selenium.firefox.GeckoDriverService .access$100(GeckoDriverService.java:38) 在 org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:112) 在 org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService. java:302) 在 org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:233) 在 org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:125) 在 org.openqa.selenium.firefox。 FirefoxDriver.(FirefoxDriver.java:121) 在 Selenium_login.(Selenium_login.java:13) 在 Selenium_login.main(Selenium_login.java:70) /home/ali/。缓存/netbeans/dev/executor-snippets/run.xml:53:Java 返回:1 BUILD FAILED(总时间:0 秒)

Java code:

爪哇代码:

import java.io.*;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_login {

    public WebDriver driver = new FirefoxDriver();

    /**
     * Open the test website.
     */
    public void openTestSite() {
        driver.navigate().to("http://testing-ground.scraping.pro/login");
    }

    /**
     * 
     * @param username
     * @param Password
     * 
     *            Logins into the website, by entering provided username and
     *            password
     */
    public void login(String username, String Password) {

        WebElement userName_editbox = driver.findElement(By.id("usr"));
        WebElement password_editbox = driver.findElement(By.id("pwd"));
        WebElement submit_button = driver.findElement(By.xpath("//input[@value='Login']"));

        userName_editbox.sendKeys(username);
        password_editbox.sendKeys(Password);
        submit_button.click();

    }

    /**
     * grabs the status text and saves that into status.txt file
     * 
     * @throws IOException
     */
    public void getText() throws IOException {
        String text = driver.findElement(By.xpath("//div[@id='case_login']/h3")).getText();
        Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("status.txt"), "utf-8"));
        writer.write(text);
        writer.close();

    }

    /**
     * Saves the screenshot
     * 
     * @throws IOException
     */
    public void saveScreenshot() throws IOException {
        File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("screenshot.png"));
    }

    public void closeBrowser() {
        driver.close();
    }

    public static void main(String[] args) throws IOException {
        Selenium_login webSrcapper = new Selenium_login();
        webSrcapper.openTestSite();
        webSrcapper.login("admin", "12345");
        webSrcapper.getText();
        webSrcapper.saveScreenshot();
        webSrcapper.closeBrowser();
    }
}

回答by kushal.8

You need to use geckodriverto interact with Firefox since Selenium 3.0. Download geckodriverfrom githubdepending upon your OS and extract geckodriver.exeinto a folder.

从 Selenium 3.0开始,您需要使用geckodriver与 Firefox 进行交互。根据您的操作系统从github下载geckodriver并解压缩到一个文件夹中。geckodriver.exe

Add the following line before initializing WebDriver:

在初始化之前添加以下行WebDriver

System.setProperty("webdriver.gecko.driver","c:/your/path/to/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
...

回答by chuha.billi

The driver constructor in selenium for example

例如,硒中的驱动程序构造函数

WebDriver driver = new FirefoxDriver();

searches for the driver executable, in this case firefox driver searches for gecko driver executable. in case the service is unable to find the executable the exception is thrown

搜索驱动程序可执行文件,在这种情况下,firefox 驱动程序搜索 gecko 驱动程序可执行文件。如果服务无法找到可执行文件,则抛出异常

this is where the exception comes from (note the check state method)

这是异常的来源(注意检查状态方法)

 /**
   *
   * @param exeName Name of the executable file to look for in PATH
   * @param exeProperty Name of a system property that specifies the path to the executable file
   * @param exeDocs The link to the driver documentation page
   * @param exeDownload The link to the driver download page
   *
   * @return The driver executable as a {@link File} object
   * @throws IllegalStateException If the executable not found or cannot be executed
   */
  protected static File findExecutable(
      String exeName,
      String exeProperty,
      String exeDocs,
      String exeDownload) {
    String defaultPath = new ExecutableFinder().find(exeName);
    String exePath = System.getProperty(exeProperty, defaultPath);
    checkState(exePath != null,
        "The path to the driver executable must be set by the %s system property;"
            + " for more information, see %s. "
            + "The latest version can be downloaded from %s",
            exeProperty, exeDocs, exeDownload);

    File exe = new File(exePath);
    checkExecutable(exe);
    return exe;
  }

Following is the check state method which throws the exception

以下是抛出异常的检查状态方法

  /**
   * Ensures the truth of an expression involving the state of the calling instance, but not
   * involving any parameters to the calling method.
   *
   * <p>See {@link #checkState(boolean, String, Object...)} for details.
   */
  public static void checkState(
      boolean b,
      @Nullable String errorMessageTemplate,
      @Nullable Object p1,
      @Nullable Object p2,
      @Nullable Object p3) {
    if (!b) {
      throw new IllegalStateException(format(errorMessageTemplate, p1, p2, p3));
    }
  }

SOLUTION: set the system property before creating driver object as follows

解决方案:在创建驱动程序对象之前设置系统属性如下

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

following is the code snippet (for firefox and chrome) where the driver service searches for the driver executable:

以下是驱动程序服务搜索驱动程序可执行文件的代码片段(用于 firefox 和 chrome):

FireFox:

火狐:

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

Chrome:

铬合金:

   @Override
    protected File findDefaultExecutable() {
      return findExecutable("chromedriver", CHROME_DRIVER_EXE_PROPERTY,
          "https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver",
          "http://chromedriver.storage.googleapis.com/index.html");
    }

where GECKO_DRIVER_EXE_PROPERTY = "webdriver.gecko.driver" and CHROME_DRIVER_EXE_PROPERTY = "webdriver.chrome.driver"

其中 GECKO_DRIVER_EXE_PROPERTY = "webdriver.gecko.driver" 和 CHROME_DRIVER_EXE_PROPERTY = "webdriver.chrome.driver"

similar is the case for other browsers, following is the snapshot of the list of the available browser implementation

其他浏览器的情况类似,以下是可用浏览器实现列表的快照

selenium browser support

硒浏览器支持