1、准备浏览器,火狐/谷歌等
2、下载驱动插件
火狐插件链接:https://github.com/mozilla/geckodriver/releases
谷歌插件:https://registry.npmmirror.com/binary.html?path=chromedriver/
https://chromedriver.storage.googleapis.com/index.html
注意与浏览器版本对应关系
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(); } }
标签:webDriver,插件,浏览器,web,selenium,火狐,Java,com From: https://www.cnblogs.com/ychun/p/17016917.html