Getting java.lang.IllegalStateException even after adding set property webdriver

徘徊边缘 提交于 2019-12-22 05:53:57

问题


I am getting 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 SeleniumFirefox.main(SeleniumFirefox.java:11)

Below is the code used SeleniumFirefox.java:

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

 public class SeleniumFirefox {

   public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.setProperty("webdriver.chromedriver.driver",
           "C://Users//balwinder//Desktop//chromedriver.exe");
    WebDriver driver = new ChromeDriver();

/*try {
    Thread.sleep(5000);
} catch(InterruptedException ex) {
    System.out.println(ex.getMessage());
}*/

   }}

回答1:


set webdriver.chrome.driver instead of webdriver.chromedriver.driver

 System.setProperty("webdriver.chromedriver.driver",
           "C://Users//balwinder//Desktop//chromedriver.exe");

Should be:

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

OR

System.setProperty("webdriver.chrome.driver",
           "C:/Users/balwinder/Desktop/chromedriver.exe");

NOTE: it will work only if you are first setting the system property and then instantiating chrome driver..




回答2:


Another solution without System.setProperty is simply add folder with chromedriver.exe to PATH (if someone don't know how - google "set path variable").

To check that this approach works you may put chromedriver.exe to C:\Windows\System32 folder.



来源:https://stackoverflow.com/questions/37404446/getting-java-lang-illegalstateexception-even-after-adding-set-property-webdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!