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

时间:2017-07-17 06:00:41

标签: java selenium geckodriver getproperty

package com.merchantPlatform;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class MerchantPlatformTest {

    public static void main(String[] args) {

        System.getProperty("webdriver.gecko.driver", "C:\\Selenium WebDriver\\geckodriver\\geckodriver-v0.17.0-win64\\geckodriver.exe");

        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability("marionette", true);

        // Initialize WebDriver
        WebDriver driver = new FirefoxDriver(capabilities);

         /* This works fine for versions lesser than Selenium 3. For Selenium 3 and higher, it will throw java.lang.IllegalStateException */

        // Maximize Window
        driver.manage().window().maximize();

        // Wait For Page To Load
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

        // Navigate to MerchantPlatform URL
        driver.get("http://localhost:52939/");

    }

}

错误

我使用System.getProperty

获得以下异常
  

线程“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:124)         在org.openqa.selenium.firefox.GeckoDriverService.access $ 100(GeckoDriverService.java:41)         在org.openqa.selenium.firefox.GeckoDriverService $ Builder.findDefaultExecutable(GeckoDriverService.java:115)         在org.openqa.selenium.remote.service.DriverService $ Builder.build(DriverService.java:330)         在org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:207)         在org.openqa.selenium.firefox.FirefoxDriver。(FirefoxDriver.java:108)         在org.openqa.selenium.firefox.FirefoxDriver。(FirefoxDriver.java:137)         在com.merchantPlatform.MerchantPlatformTest.main(MerchantPlatformTest.java:20)

2 个答案:

答案 0 :(得分:6)

您必须使用System.setProperty而不是System.getProperty,如下所示。

System.setProperty("webdriver.gecko.driver",
                "C:\\Selenium WebDriver\\geckodriver\\geckodriver-v0.17.0-win64\\geckodriver.exe");

答案 1 :(得分:1)

我注意到您使用了错误的语法来打开浏览器。

不必使用System.getProperty,您必须使用System.setProperty,如下所述。

System.setProperty("webdriver.gecko.driver", "C:\\Selenium WebDriver\\geckodriver\\geckodriver-v0.17.0-win64\\geckodriver.exe");

有关此问题的详细信息,请参阅此page