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

运行Selenium Java脚本时遭遇java.lang.ClassNotFoundException异常求助

排查Java Selenium脚本的ClassNotFoundException异常

以下是针对你遇到的java.lang.ClassNotFoundException异常的具体排查和解决步骤:

1. 补充缺失的Import语句

你的代码中使用了多个未导入的类,编译器无法找到这些类的定义,直接触发ClassNotFoundException。需要在代码开头添加以下import语句:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import java.util.List;
import java.util.ArrayList;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import io.github.bonigarcia.wdm.WebDriverManager;

2. 确认依赖库正确引入

  • Selenium Java客户端库:确保项目中已引入对应版本的selenium-java包(用Maven/Gradle的话,需在pom.xml/build.gradle中配置正确依赖)。
  • WebDriverManager依赖:你用到了WebDriverManager管理ChromeDriver,必须引入io.github.bonigarcia:webdrivermanager包,版本需与Selenium兼容(比如Selenium 4.x搭配WebDriverManager 5.x及以上版本)。
  • 手动添加jar包的话,要确保所有依赖jar都添加到项目的**构建路径(Build Path)**中。

3. 修正代码中的错误配置

  • 你同时使用System.setProperty和WebDriverManager会冲突,且System.setProperty配置存在错误:键名应为webdriver.chrome.driver(首字母小写),路径字符串多了额外双引号。建议直接删除这行代码,WebDriverManager会自动下载并配置ChromeDriver,无需手动设置。
  • 第一个循环中的打印语句放在循环体内会重复输出,建议移到循环外:
    for (int i=0; i<Listlinks.size(); i++) {
        if(Listlinks.get(i).getAttribute("href")!= null) {
            activelinks.add(Listlinks.get(i));
        }
    }
    // 移到循环外打印
    System.out.println("size of active links and images: " + activelinks.size());
    

4. 检查类路径配置

如果用IDE(如IntelliJ/Eclipse)开发,要确认:

  • 所有依赖jar已被IDE识别(Eclipse右键项目→Build Path→Add External Archives;IntelliJ添加到Libraries)
  • 项目JDK版本与依赖库兼容(建议使用JDK 11及以上版本)

修正后的完整代码示例:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import java.util.List;
import java.util.ArrayList;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import io.github.bonigarcia.wdm.WebDriverManager;

public class BrokenlinksTest {
    public static void main(String[] args) throws IOException {
        WebDriverManager.chromedriver().setup();
        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("https://facebook.com/");

        List<WebElement> Listlinks = driver.findElements(By.tagName("a"));
        System.out.println("size of links and images: " + Listlinks.size());

        List<WebElement> activelinks = new ArrayList<WebElement>();
        for (int i=0; i<Listlinks.size(); i++) {
            if(Listlinks.get(i).getAttribute("href")!= null) {
                activelinks.add(Listlinks.get(i));
            }
        }
        System.out.println("size of active links and images: " + activelinks.size());

        for (int j=0; j<activelinks.size(); j++) {
            HttpURLConnection connection = (HttpURLConnection) new URL(activelinks.get(j).getAttribute("href")).openConnection();
            connection.connect();
            String response = connection.getResponseMessage();
            connection.disconnect();
            System.out.println(j + "." + activelinks.get(j).getAttribute("href") + " - " + response);
        }
        driver.quit(); // 别忘了退出驱动
    }
}

内容的提问来源于stack exchange,提问作者vijay.k

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 02:20:24