首次运行简单Selenium脚本遇问题求助
解决Java + Selenium Eclipse环境下的模块错误与类不可访问问题
1. 处理“Module demo1 not found”错误
- 新手最快捷的解决方式是删除项目根目录的
module-info.java文件:- 在Eclipse中找到该文件
- 右键删除后刷新项目
- 若要保留模块系统,需在
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
相关产品推荐
相关产品推荐

