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

时间:2017-07-09 22:15:08

标签: java unit-testing selenium selenium-webdriver gecko

在此崩溃中评论配置结果:

thufir@doge:~/NetBeansProjects/selenium$ 
thufir@doge:~/NetBeansProjects/selenium$ gradle clean fatJar;java -jar build/libs/selenium-all.jar 

BUILD SUCCESSFUL in 1m 17s
4 actionable tasks: 4 executed
Jul 09, 2017 3:03:39 PM net.bounceme.dur.web.selenium.Main main
INFO: init..
Jul 09, 2017 3:03:41 PM net.bounceme.dur.web.selenium.Scraper scrape
INFO: {webdriver.gecko.driver=/usr/bin/firefox, url=http://www.google.com, url2=file:///home/thufir/wget/foo.html}
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
    at com.google.common.base.Preconditions.checkState(Preconditions.java:738)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124)
    at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:41)
    at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:115)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:330)
    at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:207)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:108)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:104)
    at net.bounceme.dur.web.selenium.Scraper.scrape(Scraper.java:24)
    at net.bounceme.dur.web.selenium.Main.run(Main.java:20)
    at net.bounceme.dur.web.selenium.Main.main(Main.java:15)
thufir@doge:~/NetBeansProjects/selenium$ 
thufir@doge:~/NetBeansProjects/selenium$ 

我如何或在何处整合this

{
    "capabilities": {
        "alwaysMatch": {
            "moz:firefoxOptions": {
                "binary": "/usr/local/firefox/bin/firefox",
                "args": ["--no-remote"],
                "prefs": {
                    "dom.ipc.processCount": 8
                },
                "log": {
                    "level": "trace"
                }
            }
        }
    }
}

我甚至不知道that的含义。

代码:

package net.bounceme.dur.web.selenium;

import java.util.Properties;
import java.util.logging.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Scraper {

    private static final Logger log = Logger.getLogger(Scraper.class.getName());

    public Scraper() {
    }

    public void scrape(Properties p) {
        log.info(p.toString());

        String key = "webdriver.gecko.driver";
        String url = p.getProperty("url");
        String value = p.getProperty(key);
        // System.setProperty(key, value);
        // System.setProperties(p);

        WebDriver driver = new FirefoxDriver();
        driver.get(url);
    }

}

优选地,配置将在属性文件中。该属性文件的键/值对会是什么?

1 个答案:

答案 0 :(得分:0)

您可以执行以下步骤:

  1. github.com/mozilla/geckodriver/releases

  2. 下载最新的geckodriver
  3. 检查计算机上firefox的位置。

    在Mac上Firefox位于: /Applications/Firefox.app/Contents/MacOS/firefox-bin

    在Windows上,Firefox应位于以下位置: C:\ Program Files(x86)\ Mozilla Firefox \ firefox.exe

    以下是代码:

     System.setProperty("webdriver.gecko.driver","/Users/monika/Downloads/geckodriver 2");  //the location of geckodriver on your machine
     FirefoxOptions options = new FirefoxOptions();
    options.setBinary("/Applications/Firefox.app/Contents/MacOS/firefox-bin"); //This is the location where you have installed Firefox on your machine
    options.addArguments("--no-remote");
    options.addPreference("dom.ipc.processCount",8);
    
    FirefoxDriver driver = new FirefoxDriver(options);
    driver.get("http://www.google.com");