Selenium是一个用于Web应用程序自动化测试工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera等。适用于自动化测试,js动态爬虫(破解反爬虫)等领域。
1、安装chrome浏览器
查看浏览器版本(关于Chrome):
2、下载驱动ChromeDriver.exe
驱动版本与chrome浏览器(大版本)相同即可。
https://registry.npmmirror.com/binary.html?path=chromedriver/
下载解压后,将ChromeDriver.exe随便放在一个好找的位置。
3、maven依赖
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
4、测试代码
package com.study;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Selenium {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "F:/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
ChromeDriver chrome = new ChromeDriver(options);
chrome.get("https://www.baidu.com/");
// chrome.close();
chrome.quit();
}
}
5、代理
package com.study;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
public class Selenium {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "F:/chromedriver.exe");
Proxy proxy = new Proxy();
proxy.setHttpProxy("http://192.168.111.201:8080");//代理地址
ChromeOptions options = new ChromeOptions();
options.setCapability(CapabilityType.PROXY, proxy);
ChromeDriver chrome = new ChromeDriver(options);
chrome.get("https://www.baidu.com/");//目标地址
// chrome.close();
chrome.quit();
}
}
参考: