#1 下载
通过下载地址 http://npm.taobao.org/mirrors/selenium/ , 选择对应版本下载。
2 下载驱动
通过地址 http://npm.taobao.org/mirrors/chromedriver/ 选择对应版本下载:
下载后解压,把 chromedriver.exe 复制到 chrome 安装目录下:
chromedriver与chrome的对应关系表:
chromedriver版本 支持的Chrome版本
v2.40 v66-68
v2.39 v66-68
v2.38 v65-67
v2.37 v64-66
v2.36 v63-65
v2.35 v62-64
v2.34 v61-63
v2.33 v60-62
v2.32 v59-61
v2.31 v58-60
v2.30 v58-60
v2.29 v56-58
v2.28 v55-57
v2.27 v54-56
v2.26 v53-55
v2.25 v53-55
v2.24 v52-54
v2.23 v51-53
v2.22 v49-52
v2.21 v46-50
v2.20 v43-48
v2.19 v43-47
v2.18 v43-46
v2.17 v42-43
v2.13 v42-45
v2.15 v40-43
v2.14 v39-42
v2.13 v38-41
v2.12 v36-40
v2.11 v36-40
v2.10 v33-36
v2.9 v31-34
v2.8 v30-33
v2.7 v30-33
v2.6 v29-32
v2.5 v29-32
v2.4 v29-32
#3 建立java项目
package test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com");
System.out.println(driver.getTitle());
Thread.sleep(100000);
driver.quit();
}
}
#4 运行效果
#5 使用
//赋值:
findElement.sendKeys("xxx");
#6 验证码处理
public void doPass(WebDriver driver, By moveBtn, int distance) throws InterruptedException {
WebElement div = driver.findElement(moveBtn);
Actions actions = new Actions(driver);
actions.clickAndHold(div).perform();
List<Long> list = WebDriverUtil.getTrack(distance);
for (Long long1 : list) {
actions.moveByOffset(long1.intValue(), 0).perform();
}
Thread.sleep(5);
actions.release().perform();
}
public static List<Long> getTrack(double distance) {
List<Long> track = new ArrayList<>();
double current = 0;
double mid = distance * 3 / 4;
double t = 0.2;
double v = 0;
int a = 0;
while (current < distance) {
if (current < mid) {
a = 2;
} else {
a = -3;
}
double v0 = v;
v = v0 + a * t;
double move = v0 * t + 1 / 2 * a * t * t;
current += move;
track.add(Math.round(move));
}
return track;
}