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

Selenium WebDriver运行失败:dev/failsafe/ClassNotFoundException问题求助

Selenium启动报错:java.lang.NoClassDefFoundError: dev/failsafe/Policy

问题原因

这个报错是因为你用的Selenium 4.x版本依赖Failsafe类库,但你的项目只引入了Selenium核心JAR,没添加配套的依赖JAR包。Selenium从4.x开始不再是单一JAR就能运行,必须依赖多个第三方库。

解决步骤(针对Eclipse+Java8环境)

  • 找到你下载的selenium-java-4.15.0压缩包,解压后里面有个libs文件夹,里面包含所有Selenium运行必需的依赖JAR,其中就有failsafe-*.jar
  • 打开Eclipse,右键你的项目 → 选择「Build Path」→「Add External Archives」
  • 进入selenium-java-4.15.0/libs路径,选中所有JAR文件,点击确定添加到项目中

额外优化建议

  1. 自动驱动管理(Selenium 4.6+支持):你代码里手动设置webdriver.chrome.driver的那行可以删掉,Selenium 4.6及以上版本会自动识别并下载匹配的ChromeDriver,不用再手动配置路径。
  2. 依赖管理工具(推荐):后续可以改用Maven或Gradle构建项目,只需要添加Selenium的依赖配置,就能自动下载所有需要的JAR,不用手动找依赖。比如Maven的pom.xml配置:
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.15.0</version>
</dependency>
  1. 版本匹配检查:确保ChromeDriver版本和你电脑上的Chrome浏览器主版本一致(比如Chrome 119对应ChromeDriver 119.x),避免版本不兼容问题。

修正后可运行的代码

package newAttemptPackage;

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

public class NewTryClass {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.google.com/");
        driver.manage().window().maximize();
        
        driver.close();
    }
}

内容的提问来源于stack exchange,提问作者God Gamer

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 14:15:08