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

标签 java selenium webdriver

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/

相关文章:

java - jberet,一个wildfly实例,多个数据源: can I use more than one job-repository?

java - {WebElement}.getAttribute ("value") 返回 0

javascript - 当 chrome 通过 selenium 运行时,我看不到弹出的模式

python - 使用 selenium Web 驱动器 python 单击 href=javascript 时出现问题

java - 如何通过 Selenium 捕获网页中 WebElement 的屏幕截图,而不是整个屏幕或页面

java - 如何在 Eclipse 中的 Java 构建路径中更新 Android

java - Eclipse 格式化程序设置

java - JAXB 解码到实体错误

java-selenium xpath 查找前一个节点

Java selenium 如何从谷歌搜索结果中打开链接?