简体   繁体   中英

java.lang.ExceptionInInitializerError Caused by: java.lang.IllegalStateException: The path to the driver executable must be set with Selenium and Java

public class Login 
{
    static WebDriver driver = new ChromeDriver();   
    @SuppressWarnings("resource")
    public static void main(String[] args) throws InterruptedException 
    {

        System.setProperty("webdriver.chrome.driver","C:\\Users\\MMFD-3\\MYData\\chromedriver.exe");    
        String baseUrl = "https://stackoverflow.com/";                  
        driver.get(baseUrl);
    }
}

Exception in thread "main" java.lang.ExceptionInInitializerError Caused by: java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver . The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html at com.google.common.base.Preconditions.checkState(Preconditions.java:754) at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:134) at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:32) at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137) at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:355) at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(Chrome DriverService.java:88) at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:123) at newpacakge.Login.(Login.java:14)

You are creating an instance of ChromeDriver before setting the property. Can you try after making following changes:

public class Login 
{
    static WebDriver driver;   
    @SuppressWarnings("resource")
    public static void main(String[] args) throws InterruptedException 
    {

        System.setProperty("webdriver.chrome.driver","C:\\Users\\MMFD-3\\MYData\\chromedriver.exe");
        driver = new ChromeDriver();    
        String baseUrl = "https://stackoverflow.com/";                  
        driver.get(baseUrl);
    }
}

And make sure the chrome driver version is matching with your chrome version

This error message...

Exception in thread "main" java.lang.ExceptionInInitializerError Caused by: java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver.

...implies that your program was unable to locate the chromedriver excutable.


Seems you were pretty close. Within the class defination, you can of coarse declare the WebDriver instance as follows:

static WebDriver driver;

But you mustn't instantiate it as you did:

static WebDriver driver = new ChromeDriver(); 

Unless you specifically mention the absolute path of the chromedriver executable.


Solution

You need to separate the initialization part from the declaration:

static WebDriver driver;

Initialize the webdriver as a instance of ChromeDriver() later as follows:

System.setProperty("webdriver.chrome.driver","C:\\Users\\MMFD-3\\MYData\\chromedriver.exe");  
String baseUrl = "https://stackoverflow.com/";
driver = new ChromeDriver();
driver.get(baseUrl);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Selenium:-java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property TestNG Selenium Java — java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property Selenium Java - java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property Geckodriver Error: Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by Exception in thread "main" java.lang.IllegalStateException:The path to the driver executable must be set by the : system property java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; (driver.get) Exception in thread "main" java.lang.IllegalStateException: The driver executable must exist error using Selenium and Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM