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);
}
}
线程"main"java.lang.ExceptionInInitializerError中的异常由java.lang.IollegalStateException引起:驱动程序可执行文件的路径必须由webdriver.chrome.driver系统属性设置;有关详细信息,请参阅https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver.最新版本可以从下载http://chromedriver.storage.googleapis.com/index.html网址:com.google.common.base.Prequisitions.checkState(Preconditions.java:754(网址:org.openqa.selene.remote.service.DriverService.findExecutable(DriverService.java:134(网址:org.openqa.selene.chrome.ChromeDriverService.access$000(ChromeDriverService.java:32(网址:org.openqa.selene.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137(网址:org.openqa.selene.remote.service.DriverService$Builder.build(DriverService.java:355(网址:org.openqa.selene.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88(网址:org.openqa.selene.chrome.ChromeDriver。(ChromeDriver.java:123(在newpacakge。登录。(登录.java:14(
在设置属性之前,您正在创建ChromeDriver的实例。你能在做出以下改变后尝试吗:
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);
}
}
并确保铬驱动程序版本与您的铬版本匹配
此错误消息。。。
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.
意味着您的程序无法定位chromedriver可执行程序。
看起来你很接近。在类定义中,您可以粗略地声明WebDriver实例,如下所示:
static WebDriver driver;
但你不能像你那样实例化它:
static WebDriver driver = new ChromeDriver();
除非您特别提到chromedriver可执行文件的绝对路径。
解决方案
您需要将初始化部分与声明分离:
static WebDriver driver;
稍后将webdriver
初始化为ChromeDriver()
的实例,如下所示:
System.setProperty("webdriver.chrome.driver","C:\Users\MMFD-3\MYData\chromedriver.exe");
String baseUrl = "https://stackoverflow.com/";
driver = new ChromeDriver();
driver.get(baseUrl);