环境搭建
所需工具:JDK,Eclipse,Selenium,FireFox,geckodriver || ChromeDriver,相关Jar包
版本对应, eg:
FireFox | Selenium | geckodriver |
---|---|---|
63.0.1 | 3.11.0 | 0.23.0 |
一、下载jdk&eclipse
JDK官网地址:http://www.oracle.com/technetwork/java/index.html
Eclipse下载链接: https://www.eclipse.org/downloads/
安装步骤不再赘述。
二、Selenium Tools
官网下载: https://www.seleniumhq.org/download/
其他版本下载: http://selenium-release.storage.googleapis.com/index.html
三、FireFox
各版本下载地址: http://ftp.mozilla.org/pub/firefox/releases/
四、geckodriver
Firefox驱动下载: https://github.com/mozilla/geckodriver/releases
ChromeDriver驱动下载: http://chromedriver.storage.googleapis.com/index.html
五、测试
(1)打开Eclipse,新建一个Java 项目
(2)新建好之后,选中刚刚新建的项目,右击选择buildpath-configureBuildPath:
在弹出的选项卡中选择
点击Add ExternalJars
导入三个部分的包:selenium-java-3.11.0、selenium-server-3.11.0、selenium-server-standalone-3.11.0
全部导入完成后,开始使用webdriver
(3)Eclipse 中使用WebDriver
新建一个class,代码如下:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Firefox
System.setProperty("webdriver.firefox.bin","C:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver=new FirefoxDriver();
driver.navigate().to("http://www.baidu.com");
}
}
代码的作用是打开火狐浏览器并且跳转到百度首页。
如果执行成功,说明WebDriver环境搭建完毕。
Chrome浏览器代码 如下:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Chrome
System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com/");
}
}
版权声明:本文为CSDN博主「hszxd479946」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hszxd479946/article/details/74892265