简体   繁体   中英

Exception in thread "main" java.lang.IllegalStateException:The path to the driver executable must be set by the : system property

Exception in thread "main" 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:199)  
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109)  
at org.openqa.selenium.chrome.ChromeDriverService.access$0(ChromeDriverService.java:1)  
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137)   at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)   
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)     at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:116)    
at practise_locators.DatePicker.main(DatePicker.java:11)

Here is my code:

package practise_locators;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class DatePicker {

    public static void main(String[] args){
        WebDriver driver = new ChromeDriver();
        System.setProperty("WebDriver.Chrome.driver", "E:\\chromedriver.exe");
        driver.get("https://www.google.com");
    }

}

The error says it all :

Exception in thread "main" 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:199) 

The following phrases from the error implies that there is an error in the line containing webdriver.chrome.driver

The error can be either of the following :

  • Error in the System Class Method setProperty() (including sequence) :

     System.setProperty()

    This line should be the very first line in your script.

  • Error in the specified Key :

     "WebDriver.Chrome.driver"
  • Error in the Value field :

     "E:\\chromedriver.exe"

    You have to pass the absolute path of the WebDriver through either of the following options :

    • Escaping the back slash ( \\ ) eg "C:\\path\\to\\chromedriver.exe"
    • Single forward slash ( / ) eg "C:/path/to/chromedriver.exe"

Your code seems to be having two issues as follows :

  • First issue is in specifying the Key which instead of "WebDriver.Chrome.driver" should have been "webdriver.chrome.driver" as follows :

     System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
  • Second issue is in the sequence of mentioning the Key "webDriver.chrome.driver" in your program which should be before WebDriver driver = new ChromeDriver(); as follows :

     System.setProperty("WebDriver.Chrome.driver", "E:\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.google.com");

for me the issue still exist after this: if (config.getProperty("browser").equals("chrome")) {

            System.getProperty("webdriver.chrome.driver",
                    "C:\\Selenium\\LearningProject\\DataDrivenFramework\\src\\test\\resources\\executables\\chromedriver.exe");

            driver = new ChromeDriver();

I have seen many people are using wrong sequence. the sequence should be.

  1. First set the property and then launch the browser

System.setProperty("webdriver.chrome.driver", "F:/chrome/chromedriver.exe"); WebDriver driver = new ChromeDriver();

      driver.navigate().to("https://www.google.com");

Download the chromedriver version corresponding to the chrome version in your system from https://chromedriver.chromium.org/downloads . Unzip the file and run the below code in the IDE.

import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver;

public class Selintroduction {

public static void main(String[] args) {

    //Invoking browser
System.setProperty("webdriver.chrome.driver","C:\\Users\\HP\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();

}

}

(Path would depend on your file location in the PC.)

I was facing the same error when my code was like so:

System.setProperty("webdriver.chrome.driver","C:\\Users\\abs\\chromedriver_win32.exe");

It works after adding "chromedriver" before ".exe" like so:

System.setProperty("webdriver.chrome.driver","C:\\Users\\abs\\chromedriver_win32\\chromedriver.exe");

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM