java.lang.ExceptionInInitializerError引起的错误:java.lang.IllegalStateException:必须使用Selenium和java设置驱动程序可执行文件的路径
线程“main”java.lang.ExceptionInInitializeError中出现异常 原因:java.lang.IllegalStateException:驱动程序可执行文件的路径必须由webdriver.chrome.driver系统属性设置; 有关详细信息,请参阅 . 最新的 版本可从以下网址下载: 位于com.google.common.base.premissions.checkState(premissions.java:754) 位于org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:134) 访问org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:32) 位于org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137) 位于org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:355) 位于org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88) 位于org.openqa.selenium.chrome.ChromeDriver(ChromeDriver.java:123) 在newpacakge.Login.(Login.java:14)java.lang.ExceptionInInitializerError引起的错误:java.lang.IllegalStateException:必须使用Selenium和java设置驱动程序可执行文件的路径,java,selenium,google-chrome,webdriver,selenium-chromedriver,Java,Selenium,Google Chrome,Webdriver,Selenium Chromedriver,线程“main”java.lang.ExceptionInInitializeError中出现异常 原因:java.lang.IllegalStateException:驱动程序可执行文件的路径必须由webdriver.chrome.driver系统属性设置; 有关详细信息,请参阅 . 最新的 版本可从以下网址下载: 位于com.google.common.base.premissions.checkState(premissions.java:754) 位于org.openqa.seleniu
在设置属性之前,您正在创建ChromeDriver的实例。在进行以下更改后,是否可以尝试:
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);
}
}
并确保chrome驱动程序版本与您的chrome版本匹配此错误消息
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);
}
}
…表示您的程序无法定位chromedriver可执行文件
看起来你很接近。在类定义中,可以按如下方式声明实例:
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.
static WebDriver driver;
但您不能像以前那样实例化它:
static WebDriver driver;
除非您特别提到chromedriver可执行文件的绝对路径
解决方案 您需要将初始化部分与声明分开:
static WebDriver driver = new ChromeDriver();
将webdriver
初始化为ChromeDriver()
的实例,如下所示:
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.
static WebDriver driver;
请添加您的代码,否则将不知道这是怎么回事。请添加您为修复此错误所采取的步骤。这是否回答了您的问题?您是否尝试过使用错误中的链接?这是否回答了您的问题?问题是什么?我的Chrome版本是78.0,我的Chrome驱动程序版本是78.0根据您的代码进行更改后,我在线程“main”java.lang.NoClassDefFoundError中遇到异常:okhttp3/ConnectionPool您如何添加Selenium?您应该在项目中添加selenium独立服务器jar。