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

首次运行简单Selenium脚本遇问题求助

解决Java + Selenium Eclipse环境下的模块错误与类不可访问问题

1. 处理“Module demo1 not found”错误

  • 新手最快捷的解决方式是删除项目根目录的module-info.java文件:
    1. 在Eclipse中找到该文件
    2. 右键删除后刷新项目
  • 若要保留模块系统,需在module-info.java中添加Selenium依赖声明:
    module demo1 {
        requires org.seleniumhq.selenium.core;
        requires org.seleniumhq.selenium.chrome.driver;
    }
    

2. 修正Selenium JAR包的导入路径(Classpath而非Modulepath)

  • 右键项目 → 选择Build Path → Add External Archives
  • 选中下载的Selenium文件夹内所有JAR文件(包括lib子文件夹里的全部JAR),添加到项目的Classpath(不要选Modulepath)
  • 验证配置:右键项目 → Build Path → Configure Build Path → 切换到Libraries标签,确认所有Selenium JAR都在Classpath分类下
  • 点击Apply and Close,刷新项目

3. 修正ChromeDriver路径问题

  • Mac系统下的ChromeDriver不需要.exe后缀,正确路径配置代码:
    System.setProperty("webdriver.chrome.driver", "/Users/newman/automation/chromedriver");
    
  • 额外检查:确保ChromeDriver版本与本地Chrome浏览器主版本一致(比如Chrome是118.x,ChromeDriver也要用118.x系列)

4. 验证代码与导入

  • 确保类中的导入语句正确:
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    
  • 用以下代码测试环境是否正常:
    package demoPackage;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class DemoTest {
        public static void main(String[] args) {
            System.setProperty("webdriver.chrome.driver", "/Users/newman/automation/chromedriver");
            WebDriver driver = new ChromeDriver();
            driver.get("https://www.google.com");
            driver.quit();
        }
    }
    

内容的提问来源于stack exchange,提问作者Newman Posey

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 23:31:12