当前位置:   article > 正文

webDriver以及Selenium使用总结_system.setproperty("webdriver.chrome.driver

system.setproperty("webdriver.chrome.driver

maven依赖

  1. <properties>
  2. <selenium.version>2.53.1</selenium.version>
  3. </properties>
  4. <!-- selenium -->
  5. <dependency>
  6. <groupId>org.seleniumhq.selenium</groupId>
  7. <artifactId>selenium-java</artifactId>
  8. <version>${selenium.version}</version>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.seleniumhq.selenium</groupId>
  12. <artifactId>selenium-remote-driver</artifactId>
  13. <version>${selenium.version}</version>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.seleniumhq.selenium</groupId>
  17. <artifactId>selenium-server</artifactId>
  18. <version>${selenium.version}</version>
  19. </dependency>
  20. <!-- log4j -->
  21. <dependency>
  22. <groupId>log4j</groupId>
  23. <artifactId>log4j</artifactId>
  24. <version>1.2.17</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.apache.logging.log4j</groupId>
  28. <artifactId>log4j-core</artifactId>
  29. <version>2.17.0</version>
  30. </dependency>

本地化部署

驱动下载

需要下载与chrom版本对应的chromedriver

下载位置

http://chromedriver.storage.googleapis.com/index.html

如果被墙住了,国内:

https://npm.taobao.org/mirrors/chromedriver/

查看chrome版本

chrome://version/

点击帮助-关于Google Chrome 也可以查看,但是会引起浏览器更新,不到万一不建议更新浏览器

使用样例

  1. public static void startChrome(){
  2. System.out.println("start chrome browser...");
  3. System.setProperty("webdriver.chrome.driver","C:\\chromedriver_win32_3_102.0.5\\chromedriver.exe");//指定驱动路径
  4. DesiredCapabilities capabilities = DesiredCapabilities.chrome();
  5. WebDriver driver = new ChromeDriver(capabilities);
  6. driver.get("http://www.baidu.com/");
  7. System.out.println("start firefox browser succeed...");
  8. driver.close();
  9. }

yum本地部署

Centos Chrome Chromdriver python Xvfb 无界面模式,需要centos7以上环境

安装 Xvfb

Xvfb 是一个实现了 X 服务器协议的 虚拟显示服务器,运行在内存当中,如果要运行浏览器,必须要用 X 显示服务,所以安装 Xvfb , 安装如下。

yum install Xvfb -y
yum install libXfont -y
yum install xorg-x11-fonts* -y

为防止依赖缺失,发生莫名其妙的问题,可以再执行:

yum install zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel xz xz-devel -y


安装 google-chrome

wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
yum install google-chrome-stable_current_x86_64.rpm

selenium之 chromedriver与chrome版本映射表

https://sites.google.com/a/chromium.org/chromedriver/downloads

如果被墙住了,国内:

https://npm.taobao.org/mirrors/chromedriver/


启动 Xvfb 

Xvfb -ac :99 -screen 0 1280x1024x16 & export DISPLAY=:99

手动测试下,启动google-chrome
[root@linjie test]# google-chrome

[9311:9311:0521/164942.275865:ERROR:zygote_host_impl_linux.cc(88)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.


查看chrome版本

(base) [root@localhost ~]# google-chrome --version
Google Chrome 109.0.5414.74 

找不到DevToolsActivePort的报错处理:

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

1.下载驱动

wget https://chromedriver.storage.googleapis.com/81.0.4044.69/chromedriver_linux64.zip
unzip chromedriver_linux64.zip

2.复制驱动到指定位置

chmod +x chromedriver
mv -f chromedriver /usr/local/share/chromedriver
ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
ln -s /usr/local/share/chromedriver /usr/bin/chromedriver

3.将chromedriver加入环境变量

export PATH=/usr/local/share/chromedriver:$PATH

4.添加option.add_argument('--no-sandbox')

添加option.add_argument('--no-sandbox') 即可解决找不到DevToolsActivePort的报错

在python中测试一下

  1. #coding=utf-8
  2. from selenium import webdriver
  3. option = webdriver.ChromeOptions()
  4. option.add_argument('headless')
  5. option.add_argument('--no-sandbox')
  6. driver = webdriver.Chrome(chrome_options=option)
  7. driver.get('https://www.google.com')
  8. print(driver.title)
  9. #最后关闭一下
  10. driver.quit()

来源:

安装Chrome(Headless)并在python中使用 - 腾讯云开发者社区-腾讯云

Grid部署

docker部署grid

1.启用ipv4转发

先在服务器安装好docker服务,并建议设置好国内加速器(我用的是阿里)。
由于docker需要开启端口映射,所以服务器需要启用ipv4转发:

vim /etc/sysctl.conf
#添加以下配置
net.ipv4.ip_forward=1
#重启网络服务
systemctl restart network
#查看结果为1表示已开启
sysctl net.ipv4.ip_forward

2.下载镜像

#下载hub镜像

docker pull selenium/hub

#下载chrome镜像 默认就是headless模式

docker pull selenium/node-chrome

#查看到下载到本地的镜像

docker images

3.创建网络

docker network create grid


4.创建hub容器

若不指定版本此过程会pull最新版本镜像并进行装载

docker run -d -p 4442-4444:4442-4444 --net grid --name selenium-hub selenium/hub

5.创建node容器

docker run -d --net grid -e SE_EVENT_BUS_HOST=selenium-hub  --shm-size=2g  -e SE_EVENT_BUS_PUBLISH_PORT=4442  -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443  --name selenium-node-chrme1 selenium/node-chrome

#若需多个node,可重复执行以上命令,修改--name即可,例如:

docker run -d --net grid -e SE_EVENT_BUS_HOST=selenium-hub  --shm-size=2g  -e SE_EVENT_BUS_PUBLISH_PORT=4442  -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443  --name selenium-node-chrme2 selenium/node-chrome 

6.访问控制台

http://192.168.10.130:4444/ui#

   使用样例

  1. public static void startRemoteChrome() throws MalformedURLException {
  2. System.out.println("start chrome browser...");
  3. DesiredCapabilities capabilities = DesiredCapabilities.chrome();
  4. WebDriver driver = new RemoteWebDriver(new URL("http://192.168.10.130:4444//wd/hub/"),capabilities);
  5. driver.get("http://www.baidu.com/");
  6. System.out.println("start firefox browser succeed...");
  7. driver.close();
  8. }

DesiredCapabilities配置

  1. public static WebDriver initDriver(String ip, String port, String domain, String cookieStr) {
  2. DesiredCapabilities capabilities = DesiredCapabilities.chrome();
  3. capabilities.setCapability("applicationCacheEnabled", true);
  4. capabilities.setCapability(CapabilityType.ForSeleniumServer.AVOIDING_PROXY, true);
  5. capabilities.setCapability(CapabilityType.ForSeleniumServer.ONLY_PROXYING_SELENIUM_TRAFFIC, true);
  6. // System.setProperty("http.nonProxyHosts", "localhost");
  7. Proxy proxy = new Proxy();
  8. proxy.setHttpProxy(ip + ":" + port).setFtpProxy(ip + ":" + port).setSslProxy(ip + ":" + port);
  9. capabilities.setCapability(CapabilityType.PROXY, proxy);
  10. ChromeOptions options = new ChromeOptions();
  11. options.addArguments("--proxy-server=http://" + ip + ":" + port); // 切换代理
  12. options.addArguments("--lang=" + "zh-CN"); // 切换浏览器语言
  13. // options.addArguments("--disable-javascript"); //禁用js
  14. // options.addArguments("--disable-plugins"); //禁用插件
  15. // options.addArguments("--disable-images"); //禁用图像
  16. // options.addArguments("--headless"); //-headless模式
  17. // options.addArguments("--disable-gpu"); //禁用GPU加速
  18. options.addArguments("--test-type", /* "--start-maximized", */ "no-default-browser-check"); // 意思好像是测试模式,最大化浏览器并且默认不检查浏览器
  19. //options.addArguments("--test-type", "--ignore-certificate-errors"); // 设置忽略 Chrome 浏览器证书错误报警提示
  20. // options.addArguments("user-data-dir=C:/Users/VULCAN/AppData/Local/Google/Chrome/User Data");
  21. // //设置启动chrome为默认用户的配置信息(包括书签、扩展程序、代理设置等), 运行程序前需关闭win7系统中采用默认配置打开的浏览器chrome
  22. //options.addArguments("user-data-dir=C:\\dev\\scoped_dir1808_11424");
  23. // //设置启动chrome为默认用户的配置信息(包括书签、扩展程序、代理设置等), 运行程序前需关闭win7系统中采用默认配置打开的浏览器chrome
  24. // options.addArguments("--test-type", "--incognito"); //启动进入隐身模式
  25. // options.addArguments("--test-type", "--disable-plugins"); //禁用插件
  26. //添加扩展的方法,将crx文件所在的路径添加进去
  27. // options.addExtensions(new File("C:\\Users\\swang\\AppData\\Local\\Google\\Chrome\\UserData\\Default\\Extensions\\ijaobnmmgonppmablhldddpfmgpklbfh\\1.6.0_0.crx"));
  28. //非windows环境默认用headless模式
  29. if(!windows){
  30. options.addArguments("--widows-size=1920,5000"); //为了完整截图
  31. options.addArguments("--start-fullscreen");
  32. options.addArguments("--start-maximized");
  33. //headless配置
  34. options.addArguments("--headless"); //-headless模式
  35. options.addArguments("--disable-gpu"); //禁用GPU加速
  36. options.addArguments("--no-sandbox"); //关闭沙盒模式
  37. }
  38. options.addArguments("--user-agent=" + userAgent);
  39. capabilities.setCapability(ChromeOptions.CAPABILITY, options);
  40. //WebDriver driver = new ChromeDriver(options); //本地化部署
  41. //http://x.x.x.x:4444//wd/hub/ 将url改成远程地址可以调用远程服务
  42. WebDriver driver = new RemoteWebDriver(service.getUrl(), capabilities);
  43. //设置超时
  44. driver.manage().timeouts().implicitlyWait(200, TimeUnit.SECONDS);
  45. driver.manage().timeouts().pageLoadTimeout(200, TimeUnit.SECONDS);
  46. driver.manage().timeouts().setScriptTimeout(200, TimeUnit.SECONDS);
  47. //加载cookie
  48. if (cookieStr != null && domain != null) {
  49. driver.get(domain);
  50. loadCookiesString(driver, cookieStr);
  51. driver.navigate().refresh();
  52. }
  53. return driver;
  54. }

java selenium使用封装

selenium2java基本方法二次封装 - 知乎

selenium2java调用JavaScript方法封装

SeleniumUtils

  1. package com.isi.utils;
  2. import org.apache.commons.io.FileUtils;
  3. import org.apache.log4j.Logger;
  4. import org.openqa.selenium.*;
  5. import org.openqa.selenium.chrome.ChromeDriver;
  6. import org.openqa.selenium.chrome.ChromeDriverService;
  7. import org.openqa.selenium.chrome.ChromeOptions;
  8. import org.openqa.selenium.interactions.Actions;
  9. import org.openqa.selenium.remote.CapabilityType;
  10. import org.openqa.selenium.remote.DesiredCapabilities;
  11. import org.openqa.selenium.remote.RemoteWebDriver;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.util.Arrays;
  15. import java.util.Date;
  16. import java.util.Random;
  17. import java.util.StringTokenizer;
  18. import java.util.concurrent.TimeUnit;
  19. /**
  20. * Created by VULCAN on 2017/7/4.
  21. */
  22. public class SeleniumUtils {
  23. private static String userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36";
  24. private static final Logger logger = Logger.getLogger(SeleniumUtils.class);
  25. private static boolean windows;
  26. static {
  27. String os = System.getProperty("os.name");
  28. if (os.toLowerCase().contains("windows")) {
  29. // System.setProperty("webdriver.chrome.driver", "C:/dev/chromedriver.exe");
  30. System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32_3_102.0.5\\chromedriver.exe");
  31. windows=true;
  32. } else {
  33. System.setProperty("webdriver.chrome.driver", "/home/spider/test/chromedriver");
  34. }
  35. initservice();
  36. }
  37. /**
  38. * 初始化服务
  39. */
  40. public static ChromeDriverService service=null;
  41. private static ChromeDriverService initservice() {
  42. service = new ChromeDriverService.Builder().usingDriverExecutable(new File(System.getProperty("webdriver.chrome.driver"))).usingAnyFreePort().build();
  43. try {
  44. service.start();
  45. } catch (IOException e) {
  46. e.printStackTrace();
  47. }
  48. return service;
  49. }
  50. public static synchronized WebDriver initDriver() {
  51. // ChromeDriverService service=initservice();
  52. DesiredCapabilities capabilities = DesiredCapabilities.chrome();
  53. capabilities.setCapability("applicationCacheEnabled", true);
  54. capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized", "--start-fullscreen"));
  55. ChromeOptions options = new ChromeOptions();
  56. options.addArguments("--lang=" + "zh-CN"); // 切换浏览器语言
  57. options.addArguments("--test-type", "--start-maximized" /*"no-default-browser-check"*/); // 意思好像是测试模式,最大化浏览器并且默认不检查浏览器
  58. options.addArguments("--test-type", "--ignore-certificate-errors"); // 设置忽略 Chrome 浏览器证书错误报警提示
  59. // options.addArguments("user-data-dir=C:/Users/VULCAN/AppData/Local/Google/Chrome/User Data");
  60. // //设置启动chrome为默认用户的配置信息(包括书签、扩展程序、代理设置等), 运行程序前需关闭win7系统中采用默认配置打开的浏览器chrome
  61. // options.addArguments("user-data-dir=C:\\dev\\scoped_dir25620_6285");
  62. // //设置启动chrome为默认用户的配置信息(包括书签、扩展程序、代理设置等), 运行程序前需关闭win7系统中采用默认配置打开的浏览器chrome
  63. // options.addArguments("--test-type", "--incognito"); //启动进入隐身模式
  64. // options.addArguments("--test-type", "--disable-plugins"); //禁用插件
  65. // options.addExtensions(new File("C:\\Users\\swang\\AppData\\Local\\Google\\Chrome\\UserData\\Default\\Extensions\\ijaobnmmgonppmablhldddpfmgpklbfh\\1.6.0_0.crx"));//添加扩展的方法,将crx文件所在的路径添加进去
  66. if(!windows){
  67. options.addArguments("--widows-size=1920,5000"); //为了完整截图
  68. options.addArguments("--start-fullscreen");
  69. options.addArguments("--start-maximized");
  70. //headless配置
  71. options.addArguments("--headless"); //-headless模式
  72. options.addArguments("--disable-gpu"); //禁用GPU加速
  73. options.addArguments("--no-sandbox"); //关闭沙盒模式
  74. }
  75. options.addArguments("--user-agent=" + userAgent);
  76. capabilities.setCapability(ChromeOptions.CAPABILITY, options);
  77. WebDriver driver = new ChromeDriver(options);
  78. // WebDriver driver = new RemoteWebDriver(service.getUrl(), capabilities);
  79. driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
  80. driver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
  81. driver.manage().timeouts().setScriptTimeout(100, TimeUnit.SECONDS);
  82. return driver;
  83. }
  84. /**
  85. * 设置webDriver使用的代理ip和端口port
  86. *
  87. * @param ip
  88. * @param port
  89. * @return
  90. * @throws Exception
  91. */
  92. public static WebDriver initDriver(String ip, String port, String domain, String cookieStr) {
  93. DesiredCapabilities capabilities = DesiredCapabilities.chrome();
  94. capabilities.setCapability("applicationCacheEnabled", true);
  95. capabilities.setCapability(CapabilityType.ForSeleniumServer.AVOIDING_PROXY, true);
  96. capabilities.setCapability(CapabilityType.ForSeleniumServer.ONLY_PROXYING_SELENIUM_TRAFFIC, true);
  97. // System.setProperty("http.nonProxyHosts", "localhost");
  98. Proxy proxy = new Proxy();
  99. proxy.setHttpProxy(ip + ":" + port).setFtpProxy(ip + ":" + port).setSslProxy(ip + ":" + port);
  100. capabilities.setCapability(CapabilityType.PROXY, proxy);
  101. ChromeOptions options = new ChromeOptions();
  102. options.addArguments("--proxy-server=http://" + ip + ":" + port); // 切换代理
  103. options.addArguments("--lang=" + "zh-CN"); // 切换浏览器语言
  104. // options.addArguments("--disable-javascript"); //禁用js
  105. // options.addArguments("--disable-plugins"); //禁用插件
  106. // options.addArguments("--disable-images"); //禁用图像
  107. // options.addArguments("--headless"); //-headless模式
  108. // options.addArguments("--disable-gpu"); //禁用GPU加速
  109. options.addArguments("--test-type", /* "--start-maximized", */ "no-default-browser-check"); // 意思好像是测试模式,最大化浏览器并且默认不检查浏览器
  110. //options.addArguments("--test-type", "--ignore-certificate-errors"); // 设置忽略 Chrome 浏览器证书错误报警提示
  111. // options.addArguments("user-data-dir=C:/Users/VULCAN/AppData/Local/Google/Chrome/User Data");
  112. // //设置启动chrome为默认用户的配置信息(包括书签、扩展程序、代理设置等), 运行程序前需关闭win7系统中采用默认配置打开的浏览器chrome
  113. //options.addArguments("user-data-dir=C:\\dev\\scoped_dir1808_11424");
  114. // //设置启动chrome为默认用户的配置信息(包括书签、扩展程序、代理设置等), 运行程序前需关闭win7系统中采用默认配置打开的浏览器chrome
  115. // options.addArguments("--test-type", "--incognito"); //启动进入隐身模式
  116. // options.addArguments("--test-type", "--disable-plugins"); //禁用插件
  117. //添加扩展的方法,将crx文件所在的路径添加进去
  118. // options.addExtensions(new File("C:\\Users\\swang\\AppData\\Local\\Google\\Chrome\\UserData\\Default\\Extensions\\ijaobnmmgonppmablhldddpfmgpklbfh\\1.6.0_0.crx"));
  119. //非windows环境默认用headless模式
  120. if(!windows){
  121. options.addArguments("--widows-size=1920,5000"); //为了完整截图
  122. options.addArguments("--start-fullscreen");
  123. options.addArguments("--start-maximized");
  124. //headless配置
  125. options.addArguments("--headless"); //-headless模式
  126. options.addArguments("--disable-gpu"); //禁用GPU加速
  127. options.addArguments("--no-sandbox"); //关闭沙盒模式
  128. }
  129. options.addArguments("--user-agent=" + userAgent);
  130. capabilities.setCapability(ChromeOptions.CAPABILITY, options);
  131. //WebDriver driver = new ChromeDriver(options); //本地化部署
  132. //http://x.x.x.x:4444//wd/hub/ 将url改成远程地址可以调用远程服务
  133. WebDriver driver = new RemoteWebDriver(service.getUrl(), capabilities);
  134. //设置超时
  135. driver.manage().timeouts().implicitlyWait(200, TimeUnit.SECONDS);
  136. driver.manage().timeouts().pageLoadTimeout(200, TimeUnit.SECONDS);
  137. driver.manage().timeouts().setScriptTimeout(200, TimeUnit.SECONDS);
  138. //加载cookie
  139. if (cookieStr != null && domain != null) {
  140. driver.get(domain);
  141. loadCookiesString(driver, cookieStr);
  142. driver.navigate().refresh();
  143. }
  144. return driver;
  145. }
  146. public static void loadCookiesString(WebDriver driver, String cookieConent) {
  147. String lineArr[] = cookieConent.split("\n");
  148. for (String line : lineArr) {
  149. StringTokenizer str = new StringTokenizer(line, ";");
  150. while (str.hasMoreTokens()) {
  151. String name = str.nextToken();
  152. String value = str.nextToken();
  153. String domain = str.nextToken();
  154. String path = str.nextToken();
  155. Date expiry = null;
  156. String dt;
  157. if (!(dt = str.nextToken()).equals(null)) {
  158. // expiry=new Date(dt);
  159. // System.out.println();
  160. }
  161. boolean isSecure = new Boolean(str.nextToken()).booleanValue();
  162. Cookie ck = new Cookie(name, value, domain, path, expiry, isSecure);
  163. driver.manage().addCookie(ck);
  164. }
  165. }
  166. driver.navigate().refresh();
  167. }
  168. public static String getCookieStr(WebDriver driver) {
  169. // driver.navigate().refresh();
  170. String cookieStr = null;
  171. for (Cookie ck : driver.manage().getCookies()) {
  172. cookieStr += (ck.getName() + "=" + ck.getValue() + ";" + ck.getDomain() + ";" + ck.getPath() + ";" + ck.getExpiry() + ";" + ck.isSecure() + ";");
  173. // cookieStr += ck.getName() + "=" + ck.getValue()+";";
  174. }
  175. if(cookieStr==null) {
  176. return null;
  177. }else {
  178. return cookieStr.substring(0, cookieStr.length() - 1).replaceFirst("null","");
  179. }
  180. }
  181. public static String getTotalCookieStr(WebDriver driver) {
  182. driver.navigate().refresh();
  183. String cookieStr = driver.manage().getCookies().toString();
  184. cookieStr = cookieStr.substring(1, cookieStr.length() - 1);
  185. return cookieStr;
  186. }
  187. /**
  188. * 控制页面滚动
  189. *
  190. * @param driver
  191. * @param pageNum
  192. * @param waitTime
  193. */
  194. private static Random random = new Random();
  195. public static void rollPage(WebDriver driver, int pageNum, int waitTime) {
  196. // 控制滚动条一直向下翻页
  197. String url = driver.getCurrentUrl();
  198. JavascriptExecutor js = (JavascriptExecutor) driver;
  199. for (int i = 0; i < pageNum; i++) {
  200. //js控制浏览器划到底部
  201. js.executeScript("window.scrollTo(0,document.body.scrollHeight)");
  202. logger.info(url + " page num::::::::" + (i + 1));
  203. sleep(waitTime + random.nextInt(5000));
  204. }
  205. }
  206. /**
  207. * 关闭 解决进程残留问题
  208. *
  209. * @param driver
  210. */
  211. public static synchronized void close(WebDriver driver) {
  212. if (driver != null) {
  213. try {
  214. driver.quit();
  215. driver.close();
  216. // service.stop();
  217. } catch (Exception e) {
  218. logger.warn("close webdriver error " + e.getMessage());
  219. }
  220. }
  221. }
  222. public static File getTotalImg(WebDriver driver) throws IOException {
  223. driver.manage().window().fullscreen();
  224. JavascriptExecutor js = (JavascriptExecutor) driver;
  225. // Object width=js.executeScript("return document.body.scrollWidth");
  226. // Object height=js.executeScript("return document.body.scrollHeight");
  227. Object width = js.executeScript("return (document.documentElement.scrollWidth>document.documentElement.clientWidth) ? document.documentElement.scrollWidth : document.documentElement.scrollWidth");
  228. Object height = js.executeScript("return (document.documentElement.scrollHeight >document.documentElement.clientHeight) ? document.documentElement.scrollHeight : document.documentElement.clientHeight");
  229. int w = Integer.parseInt(width.toString());
  230. int h = Integer.parseInt(height.toString());
  231. System.out.println("width::::" + width);
  232. System.out.println("height::::" + height);
  233. driver.manage().window().setSize(new Dimension(w, h));
  234. TakesScreenshot takesScreenshot = (TakesScreenshot) driver;
  235. return takesScreenshot.getScreenshotAs(OutputType.FILE);
  236. }
  237. /**
  238. * 整页截图
  239. *
  240. * @param driver
  241. * @param destPath
  242. * @throws IOException
  243. */
  244. public synchronized static void screenshot(WebDriver driver, String destPath) throws IOException {
  245. //记录原有的windows size
  246. Dimension winSize = driver.manage().window().getSize();
  247. File outFile = getTotalImg(driver);
  248. FileUtils.moveFile(outFile, new File(destPath));
  249. //还原windows size
  250. driver.manage().window().setSize(winSize);
  251. }
  252. /**
  253. * 选中目标右键
  254. * @param driver
  255. * @param tagName
  256. */
  257. public static void contextClickByTagName(WebDriver driver,String tagName) {
  258. Actions action = new Actions(driver);
  259. action.contextClick(driver.findElement(By.tagName(tagName))).build().perform();
  260. }
  261. public static void sleep(int time){
  262. try {
  263. Thread.sleep(time);
  264. } catch (InterruptedException e) {
  265. e.printStackTrace();
  266. }
  267. }
  268. public static void main(String[] args) throws IOException {
  269. // for(int i=0;i<10;i++){
  270. // WebDriver webDriver = initDriver();
  271. // driver.get("http://www.baidu.com");
  272. // ThreadUtil.sleep(1000);
  273. // driver.quit();
  274. // }
  275. //加载 cookie
  276. // QuestCookieDao questCookieDao = AppUtils.daoFactory(QuestCookieDao.class);
  277. // QuestCookie questCookie = questCookieDao.getCookiesByRand2();
  278. // us.codecraft.webmagic.proxy.Proxy proxy = ProxyUtils.getProxyByDomain();
  279. // String host = proxy.getHost();
  280. // String port = String.valueOf(proxy.getPort());
  281. // String cookies = questCookie.getCookies();
  282. // SeleniumUtils.initDriver(host,port,"http://www.zujuan.com/",cookies);
  283. WebDriver driver = initDriver();
  284. String url = "http://www.bubuko.com/infodetail-186319.html";
  285. driver.get(url);
  286. screenshot(driver, "D:/test.png");
  287. // sleep(10000);
  288. // String url2 = "http://www.qichacha.com/search?key=腾讯";
  289. // driver.navigate().to(url2);
  290. // String cookieStr = getCookieStr(driver);
  291. // System.out.println(cookieStr);
  292. // driver.navigate().refresh();
  293. // System.out.println("--------------------------------");
  294. // cookieStr = getCookieStr(driver);
  295. // System.out.println(cookieStr);
  296. }
  297. }

Selenium反屏蔽

python关于屏蔽selenium指纹的文章有很多都是使用stealth.min.js来屏蔽特征的,stealth.min.js又是node.js用来屏蔽selenium指纹的一段js代码,所以可以说java,和python屏蔽selenium特征的最初来源是node.js

依赖

  1. <properties>
  2. <selenium.version>4.2.2</selenium.version>
  3. </properties>
  4. <dependencies>
  5. <!-- selenium -->
  6. <dependency>
  7. <groupId>org.seleniumhq.selenium</groupId>
  8. <artifactId>selenium-java</artifactId>
  9. <version>${selenium.version}</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.seleniumhq.selenium</groupId>
  13. <artifactId>selenium-remote-driver</artifactId>
  14. <version>${selenium.version}</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.seleniumhq.selenium</groupId>
  18. <artifactId>selenium-server</artifactId>
  19. <version>2.53.1</version>
  20. </dependency>
  21. <!--hutool-->
  22. <dependency>
  23. <groupId>cn.hutool</groupId>
  24. <artifactId>hutool-all</artifactId>
  25. <version>5.6.0</version>
  26. </dependency>
  27. </dependencies>

代码

  1. import cn.hutool.core.io.FileUtil;
  2. import cn.hutool.core.map.MapBuilder;
  3. import org.openqa.selenium.Dimension;
  4. import org.openqa.selenium.WebDriver;
  5. import org.openqa.selenium.chrome.ChromeDriver;
  6. import org.openqa.selenium.chrome.ChromeOptions;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.util.Arrays;
  10. import java.util.HashMap;
  11. import java.util.LinkedHashMap;
  12. import java.util.Map;
  13. public class BotTest {
  14. public static void main(String[] args) throws IOException {
  15. WebDriver driver = getWebDriver();
  16. driver.get("https://bot.sannysoft.com/");
  17. }
  18. /**
  19. * 获取web驱动
  20. *
  21. * @return 浏览器驱动
  22. */
  23. public static WebDriver getWebDriver() throws IOException {
  24. String webDriverDir = "C:\\chromedriver_win32_3_102.0.5\\chromedriver.exe";
  25. // 加载驱动
  26. System.setProperty("webdriver.chrome.driver", webDriverDir);
  27. // 设置浏览器参数
  28. ChromeOptions options = new ChromeOptions();
  29. Map<String, Object> prefs = new HashMap<String, Object>();
  30. prefs.put("credentials_enable_service", false);
  31. prefs.put("profile.password_manager_enabled", false);
  32. // excludeSwitches", Arrays.asList("enable-automation")在高版本的谷歌浏览器是无法屏蔽
  33. // window.navigator.webdriver 为false 的特征,这里写出来是为了配合其他参数来关闭浏览器上显示"正在收到自动测试软件控制"的提示
  34. options.setExperimentalOption("excludeSwitches", Arrays.asList("enable-automation"));
  35. options.addArguments("--disable-blink-features");
  36. options.addArguments("--disable-blink-features=AutomationControlled");
  37. options.setExperimentalOption("useAutomationExtension", false);
  38. options.setExperimentalOption("prefs", prefs);
  39. // 创建驱动对象
  40. WebDriver driver = new ChromeDriver(options);
  41. driver.manage().window().setSize(new Dimension(1280, 1024));
  42. // 去除seleium全部指纹特征
  43. String js = FileUtil.readString(new File("C:\\tmp\\team.min.js"), "utf-8");
  44. // MapBuilder是依赖hutool工具包的api
  45. Map<String, Object> commandMap = MapBuilder.create(new LinkedHashMap<String, Object>()).put("source", js)
  46. .build();
  47. // executeCdpCommand这个api在selenium3中是没有的,请使用selenium4才能使用此api
  48. ((ChromeDriver) driver).executeCdpCommand("Page.addScriptToEvaluateOnNewDocument", commandMap);
  49. return driver ;
  50. }
  51. }

测试网站

Antibothttps://bot.sannysoft.com/

 stealth.min.js

https://github.com/requireCool/stealth.min.js

PhantomJs使用总结

PhantomJS是一个基于webkit的JavaScript API。它使用QtWebKit作为它核心浏览器的功能,使用webkit来编译解释执行JavaScript代码。

phantomJs使用总结_csdncjh的博客-CSDN博客下载 | PhantomJS 使用封装PhantomJsUtils来源phantomJs_Selenium_java 最全配置访问_菜鸡java程序员的博客-CSDN博客Selenium+Phantomjs做Java爬虫_西红柿丶番茄的博客-CSDN博客_java phantomjs seleniumhttps://blog.csdn.net/csdncjh/article/details/125382427

来源

Selenium的四种部署方式 - 知乎

Selenium Grid Docker部署+PythonDemo+配置解析

java selenium屏蔽所有selenium指纹和特征_fx9590的博客-CSDN博客_selenium去掉特征

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/149876
推荐阅读
相关标签
  

闽ICP备14008679号