即使在添加 set 属性 webdriver 后仍获取 java.lang.IllegalStateException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37404446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:17:11  来源:igfitidea点击:

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

javaseleniumselenium-webdriverwebdriverselenium-chromedriver

提问by balvinder dhillon

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
 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());
}*/

   }}
(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:

下面是使用SeleniumFirefox.java的代码:

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

采纳答案by user861594

set webdriver.chrome.driverinstead of webdriver.chromedriver.driver

设置webdriver.chrome.driver而不是webdriver.chromedriver.driver

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

Should be:

应该:

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

OR

或者

##代码##

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

注意:只有当您首先设置系统属性然后实例化 chrome 驱动程序时,它才会起作用。

回答by Alexander P.

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

另一个没有的解决方案System.setProperty是简单地将文件夹添加chromedriver.exe到 PATH (如果有人不知道如何 - 谷歌“设置路径变量”)。

To check that this approach works you may put chromedriver.exeto C:\Windows\System32folder.

要检查,这种方法工作你可以将chromedriver.exeC:\Windows\System32文件夹中。