线程 "main" java.lang.IllegalStateException 中的异常:使用 Selenium ChromeDriver 和 Java 时驱动程序可执行文件必须存在错误

Exception in thread "main" java.lang.IllegalStateException: The driver executable must exist error using Selenium ChromeDriver and Java

我正面临 java.lang.IllegalStateException 执行我的代码。

代码试验:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class seleniumintroduction {

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver","C:\Users\ATIF\OneDrive\Desktop\Selenium Course\chromedriver_win32.exe");
        WebDriver driver=new ChromeDriver();
    }
 }

我看到的错误:

Exception in thread "main" java.lang.IllegalStateException: The driver executable must exist: C:\Users\ATIF\OneDrive\Desktop\Selenium Course\chromedriver_win32.exe

虽然下载的 zip 文件名是 chromedriver_win32.zip,但是一旦你解压 它就是 chromedriver.exe

因此您需要更改行:

System.setProperty("webdriver.chrome.driver","C:\Users\ATIF\OneDrive\Desktop\Selenium Course\chromedriver_win32.exe");

作为

System.setProperty("webdriver.chrome.driver","C:\Users\ATIF\OneDrive\Desktop\Selenium Course\chromedriver.exe");