You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行Selenium代码时遇NoClassDefFoundError错误求助

解决Java Selenium代码运行时的NoClassDefFoundError问题

java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver 这个错误的核心原因是项目未正确引入Selenium核心依赖库,导致JVM在运行时找不到WebDriver类。

解决步骤

1. 确保Selenium依赖已正确添加

根据你的项目构建工具选择对应方式:

  • Maven项目:在pom.xml中添加上Selenium Java依赖(替换为当前稳定版本,如4.15.0):

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.15.0</version>
        </dependency>
    </dependencies>
    

    执行mvn clean install刷新依赖。

  • Gradle项目:在build.gradle的依赖块中添加:

    dependencies {
        implementation 'org.seleniumhq.selenium:selenium-java:4.15.0'
    }
    

    同步Gradle项目。

  • 手动导入Jar包:

    • 下载对应版本的Selenium Java压缩包
    • 解压后将根目录的selenium-java-xxx.jar和libs文件夹下的所有依赖Jar,添加到项目的类路径中(IDE中通过"添加外部Jar"操作完成)。

2. 修正代码中的键名错误

你的代码中System.setProperty的键名写错了,正确的键名是小写开头的webdriver.chrome.driver,修正后代码如下:

package TEST_Automation;

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

public class Test_1 {

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Google\\Chrome\\Application\\chrome\\chromedriver.exe");

        WebDriver driver = new ChromeDriver();
        driver.get("https://www.google.com");
        // 执行完操作后关闭浏览器,避免资源占用
        driver.quit();
    }

}

内容的提问来源于stack exchange,提问作者Atul Nachan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 05:43:32