搭建web自动化环境,selenium-Java+火狐浏览器+idea
1、准备浏览器,火狐/谷歌等
2、下载驱动插件
火狐插件链接:https://github.com/mozilla/geckodriver/releases
谷歌插件:https://registry.npmmirror.com/binary.html?path=chromedriver/
https://chromedriver.storage.googleapis.com/index.html
最新版谷歌插件 https://googlechromelabs.github.io/chrome-for-testing/
注意与浏览器版本对应关系
3、打开idea,创建项目,添加依赖,并测试程序
<dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.7.2</version> </dependency> </dependencies>
编写程序进行测试
public class WebDriverTest { public static void main(String[] args) throws InterruptedException { // 加载驱动 System.setProperty("webdriver.gecko.driver","E:\\BaiduNetdiskDownload\\geckodriver.exe"); // 指明浏览器位置,否则会报【Caused by: org.openqa.selenium.WebDriverException: Driver server process died prematurely.】 System.setProperty("webdriver.firefox.bin","C:\\Program Files\\Mozilla Firefox\\firefox.exe"); WebDriver webDriver = new FirefoxDriver() ; // 打开百度 webDriver.get("http://www.baidu.com/"); // 在搜索框输入内容 webDriver.findElement(By.id("kw")).sendKeys("这里输入搜索内容"); // 停留2S Thread.sleep(2000); //关闭驱动,以及所有相关窗口 webDriver.quit(); } }
Driver server process died prematurely
这个错误通常出现在使用Selenium WebDriver时,它表示WebDriver的服务器进程意外地提前终止了。这可能是由于许多不同的原因造成的,比如:
版本不匹配:Selenium WebDriver和浏览器驱动程序之间的版本不匹配可能导致此问题。
驱动程序问题:浏览器驱动程序本身可能存在问题,可以尝试更新到最新版本。
系统配置问题:可能是由于系统配置或环境问题引起的,比如网络问题、权限问题等。
为了解决这个问题,你可以尝试以下几个步骤:
更新WebDriver和浏览器:确保你使用的WebDriver和浏览器是最新版本。
检查日志:查看WebDriver和浏览器驱动程序的日志,看看是否有任何错误信息。
检查系统配置:确保系统配置和环境设置正确,包括网络连接、权限等。