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

java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property

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

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.access0(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.(FirefoxDriver.java:108) at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:137) at com.merchantPlatform.MerchantPlatformTest.main(MerchantPlatformTest.java:20)

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

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

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

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

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

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