gpt4 book ai didi

java - 为什么我无法为我的 selenium webdriver (java) 设置系统属性?

转载 作者:行者123 更新时间:2023-12-01 17:54:04 25 4
gpt4 key购买 nike

public class Main {

static Locators Locators = new Locators();
static WebDriver driver = new ChromeDriver();

public static void main(String[] args) {

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

我遇到以下异常:

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. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
at com.google.common.base.Preconditions.checkState(Preconditions.java:847)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:134)
at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:35)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:159)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:355)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:94)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
at Main.<clinit>(Main.java:11)

在这一行:

static WebDriver driver = new ChromeDriver();

虽然我已经定义了 sys.setproperty,但为什么我会收到此消息?如何在代码中修复它?

最佳答案

正如 @Xiao Yu 提到的,您尝试在系统属性中设置路径之前创建 chrome 驱动程序实例。您可以更新代码,如下所示

public class Main {
static Locators Locators = new Locators();
static WebDriver driver;

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\currentuser\\chromedriver.exe");
driver = new ChromeDriver();
}

关于java - 为什么我无法为我的 selenium webdriver (java) 设置系统属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60748555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com