当前位置:   article > 正文

从头学习爬虫(十二)实战篇----新版极验测试

极验测试

本文主要测试新版极验,掌握一些selenium的功能,还有一些不在实战了,例如headless。。自行百度

  1. package q;
  2. import java.awt.image.BufferedImage;
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.ByteArrayOutputStream;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.OutputStream;
  13. import java.net.HttpURLConnection;
  14. import java.net.URL;
  15. import java.net.URLConnection;
  16. import java.time.Duration;
  17. import java.util.ArrayList;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import java.util.Random;
  21. import java.util.Set;
  22. import javax.imageio.ImageIO;
  23. import javax.script.Invocable;
  24. import javax.script.ScriptEngine;
  25. import javax.script.ScriptEngineManager;
  26. import org.apache.http.HttpEntity;
  27. import org.apache.http.HttpHost;
  28. import org.apache.http.client.ClientProtocolException;
  29. import org.apache.http.client.config.RequestConfig;
  30. import org.apache.http.client.methods.CloseableHttpResponse;
  31. import org.apache.http.client.methods.HttpGet;
  32. import org.apache.http.impl.client.CloseableHttpClient;
  33. import org.apache.http.impl.client.DefaultHttpClient;
  34. import org.apache.http.impl.client.HttpClientBuilder;
  35. import org.apache.http.impl.client.HttpClients;
  36. import org.apache.http.impl.conn.PoolingClientConnectionManager;
  37. import org.apache.http.util.EntityUtils;
  38. import org.junit.Test;
  39. import org.openqa.selenium.By;
  40. import org.openqa.selenium.JavascriptExecutor;
  41. import org.openqa.selenium.Point;
  42. import org.openqa.selenium.WebDriver;
  43. import org.openqa.selenium.WebElement;
  44. import org.openqa.selenium.chrome.ChromeDriver;
  45. import org.openqa.selenium.chrome.ChromeOptions;
  46. import org.openqa.selenium.interactions.Actions;
  47. import sun.misc.BASE64Decoder;
  48. /**
  49. * 新版本极验官网测试旧版滑块算法(失败)
  50. * 有需求可对接打码平台
  51. *
  52. */
  53. public class JiYan {
  54. public static void main(String[] args) throws Exception {
  55. run1();
  56. }
  57. public static void decodeBase64ToImage(String base64) {
  58. BASE64Decoder decoder = new BASE64Decoder();
  59. try {
  60. File file = new File("E:\\a1.png");
  61. if (file.exists()) {
  62. file.delete();
  63. }
  64. FileOutputStream write = new FileOutputStream(new File("E:\\a1.png"));
  65. byte[] decoderBytes = decoder.decodeBuffer(base64.replace("data:image/png;base64,", ""));
  66. write.write(decoderBytes);
  67. write.close();
  68. } catch (IOException e) {
  69. e.printStackTrace();
  70. }
  71. }
  72. public static void decodeBase64ToImage1(String base64) {
  73. BASE64Decoder decoder = new BASE64Decoder();
  74. try {
  75. File file = new File("E:\\a2.png");
  76. if (file.exists()) {
  77. file.delete();
  78. }
  79. FileOutputStream write = new FileOutputStream(new File("E:\\a2.png"));
  80. byte[] decoderBytes = decoder.decodeBuffer(base64.replace("data:image/png;base64,", ""));
  81. write.write(decoderBytes);
  82. write.close();
  83. } catch (IOException e) {
  84. e.printStackTrace();
  85. }
  86. }
  87. public static void doGet(String url) throws Exception {
  88. DefaultHttpClient httpclient = new DefaultHttpClient(new PoolingClientConnectionManager());
  89. HttpGet httpget = new HttpGet(url);
  90. CloseableHttpResponse response = null;
  91. HttpEntity entity = null;
  92. String s = "";
  93. try {
  94. httpget.addHeader("User-Agent",
  95. "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36");
  96. response = httpclient.execute(httpget);
  97. entity = response.getEntity();
  98. System.out.println("doGet" + response.getStatusLine().getStatusCode());
  99. s = EntityUtils.toString(entity, "UTF-8");
  100. } catch (ClientProtocolException e) {
  101. e.printStackTrace();
  102. } catch (IOException e) {
  103. e.printStackTrace();
  104. } finally {
  105. httpclient.close();
  106. }
  107. }
  108. // 判断像素是否相差过大
  109. private static boolean isSimilar(BufferedImage image1, BufferedImage image2, int x, int y) {
  110. int pixel1 = image1.getRGB(x, y);
  111. int pixel2 = image2.getRGB(x, y);
  112. int[] rgb1 = getRGB(pixel1);
  113. int[] rgb2 = getRGB(pixel2);
  114. for (int i = 0; i < 3; i++) {
  115. if (Math.abs(rgb1[i] - rgb2[i]) > 80) {
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. // 返回RGB数组
  122. private static int[] getRGB(int pixel) {
  123. int[] rgb = new int[3];
  124. rgb[0] = (pixel & 0xff0000) >> 16;
  125. rgb[1] = (pixel & 0xff00) >> 8;
  126. rgb[2] = (pixel & 0xff);
  127. return rgb;
  128. }
  129. // 计算移动距离
  130. private static int getDiffLocation(BufferedImage image1, BufferedImage image2) {
  131. int i = 0;
  132. for (int x = 0; x < 260; x++) {
  133. for (int y = 0; y < 116; y++) {
  134. if (isSimilar(image1, image2, x, y) == false) {
  135. return x;
  136. }
  137. }
  138. }
  139. return i;
  140. }
  141. public static void run1() throws Exception {
  142. System.getProperties().setProperty("webdriver.chrome.driver", "D:\\chromedriver_win32(1)\\chromedriver.exe");
  143. ChromeOptions options = new ChromeOptions();
  144. options.setBinary("C:\\Program Files\\Chrome\\Chrone.exe");
  145. WebDriver driver = new ChromeDriver(options);
  146. // driver.manage().window().maximize();
  147. driver.get("http://www.geetest.com/type/");
  148. //暴力延迟
  149. Thread.sleep(1000);
  150. driver.findElement(By.xpath("//div[@class='products-content']/ul/li[3]")).click();
  151. Thread.sleep(1000);
  152. driver.findElement(By.xpath("//div[@class='products-content']/ul/li[2]")).click();
  153. Thread.sleep(1000);
  154. driver.findElement(By.xpath("//div[@class='geetest_radar_btn']/div[@class='geetest_radar_tip']/span")).click();
  155. System.out.println(driver.findElement(By.xpath("//div[@class='geetest_radar_tip']/span")).getText());
  156. Thread.sleep(1000);
  157. //执行js 获取验证码图片 画布转base64
  158. JavascriptExecutor js = (JavascriptExecutor) driver;
  159. String jsstr = "var oCanvas =document.getElementsByClassName('geetest_canvas_fullbg geetest_fade geetest_absolute')[0];return oCanvas.toDataURL();";
  160. String o = (String) js.executeScript(jsstr);
  161. decodeBase64ToImage(o);
  162. //System.out.println(o);
  163. String jsstr1 = "var oCanvas =document.getElementsByClassName('geetest_canvas_bg geetest_absolute')[0];return oCanvas.toDataURL();";
  164. String o1 = (String) js.executeScript(jsstr1);
  165. decodeBase64ToImage1(o1);
  166. // driver.findElement(By.className("geetest_radar_tip_content")).click();
  167. // 获取移动距离
  168. Random random = new Random();
  169. BufferedImage image1 = ImageIO.read(new FileInputStream("E:/a1.png"));
  170. BufferedImage image2 = ImageIO.read(new FileInputStream("E:/a2.png"));
  171. int ranAddLoc = -2;
  172. int loc = (getDiffLocation(image1, image2) - 5) + ranAddLoc;
  173. System.out.println(loc);
  174. //移动算法
  175. List<Integer> trackList = getTrackList3(loc);
  176. Thread.sleep(200);
  177. // 找到滑动的圆球
  178. WebElement slider = driver.findElement(By.xpath("//div[@class='geetest_slider_button']"));
  179. int y = slider.getSize().height;
  180. // 鼠标按住滑块
  181. Actions actions = new Actions(driver);
  182. actions.clickAndHold(slider).perform();
  183. int a = 0;
  184. Point start =slider.getLocation();
  185. System.out.println(slider.getLocation().toString());
  186. //actions.moveToElement(slider,loc, y).perform();
  187. Thread.sleep(500+random.nextInt(500));
  188. //actions.moveToElement(slider,200, y).perform();
  189. //transform: translate(0px, 0px);
  190. //div.style.transform = "translate(500px)";
  191. /*String jsstr3 = "var e =document.getElementsByClassName('geetest_slider_button')[0];e.style.transform = 'translate("+100+"px,0px)';";
  192. js.executeScript(jsstr3);*/
  193. //Thread.sleep(1000);
  194. //System.out.println(slider.getLocation().toString());
  195. //actions.moveToElement(slider,0, y).perform();
  196. /*String jsstr4 = "var d =document.getElementsByClassName('geetest_slider_button')[0];d.style.transform = 'translate("+20+"px,0px)';";
  197. js.executeScript(jsstr4);*/
  198. //Thread.sleep(1000);
  199. System.out.println(slider.getLocation().toString());
  200. for (int i = 0; i < trackList.size(); i++) {
  201. a += trackList.get(i);
  202. //System.out.println("滑了" + a);
  203. actions.clickAndHold(slider).moveByOffset(trackList.get(i), 0);
  204. if(i<trackList.size()*4/5) {
  205. actions.pause(random.nextInt(20)+100);
  206. }else {
  207. actions.pause(random.nextInt(20)+200);
  208. }
  209. // actions.wait(200);
  210. //actions.moveToElement(slider,a, y - random.nextInt(1)-5).build().perform();
  211. //actions.moveToElement(slider).moveByOffset(xOffset, yOffset)
  212. //Thread.sleep(1000);
  213. //System.out.println(slider.getLocation().toString());
  214. /*if(slider.getLocation().getX()-start.getX()-loc>-20) {
  215. actions.moveToElement(slider,loc, y - random.nextInt(1)-5).build().perform();
  216. System.out.println("callback");
  217. break;
  218. }*/
  219. }
  220. //Thread.sleep(200);
  221. System.out.println("释放前"+slider.getLocation().toString());
  222. actions.release(slider).build().perform();
  223. System.out.println("释放后"+slider.getLocation().toString());
  224. }
  225. //获取轨迹
  226. public static List<Integer> getTrackList(int loc) {
  227. List<Integer> list = new ArrayList<>();
  228. list.add(loc * 4 / 15);
  229. list.add(loc * 2 / 15);
  230. list.add(loc * 4 / 15);
  231. list.add(loc * 5 / 24);
  232. list.add(loc - (loc * 4 / 15 + loc * 2 / 15 + loc * 4 / 15 + loc * 5 / 24));
  233. /*
  234. * list.add(loc*2/3); list.add(loc-loc*2/3);
  235. */
  236. /*
  237. * System.out.println(loc*4/15+loc*2/15+loc*4/15+loc*5/24+loc-(loc*4/15+loc*2/15
  238. * +loc*4/15+loc*5/24)); System.out.println(loc);
  239. */
  240. return list;
  241. }
  242. //获取轨迹2
  243. @Test
  244. public void getTrackList2() {
  245. int loc;
  246. loc=100;
  247. List<Integer> list = new ArrayList<>();
  248. double mid = loc * 4.0 / 5;
  249. // 计算间隔
  250. double t = 0.2;
  251. // 初速度
  252. double v = 0.0;
  253. double current = 0.0;
  254. while (current < loc) {
  255. double a;
  256. if (current < mid) {
  257. // 加速度为正2
  258. a = loc*1.0;
  259. } else {
  260. // 加速度为负3
  261. a = -loc*1.5;
  262. }
  263. // 初速度v0
  264. double v0 = v;
  265. // 当前速度v = v0 + at
  266. v = v0 + a * t;
  267. // 移动距离x = v0t + 1/2 * a * t^2
  268. double move = v0 * t + 1 / 2 * a * t * t;
  269. // 当前位移
  270. current += move;
  271. // 加入轨迹
  272. // int mov1=;
  273. System.out.println(move);
  274. System.out.println(" "+current);
  275. list.add((int) (move));
  276. }
  277. //return null;
  278. }
  279. //获取轨迹3
  280. // @Test
  281. public static List<Integer> getTrackList3(int loc) {
  282. /*int loc;
  283. loc=100;*/
  284. List<Integer> list = new ArrayList<>();
  285. int mid = loc * 4/ 5;
  286. // 计算间隔
  287. double t = 0.2;
  288. // 初速度
  289. double v = 0;
  290. int current = 0;
  291. while (current < loc) {
  292. double a;
  293. if (current < mid) {
  294. // 加速度为正2
  295. a = loc*1.0;
  296. } else {
  297. // 加速度为负3
  298. a = -loc*1.5;
  299. }
  300. // 初速度v0
  301. double v0 = v;
  302. // 当前速度v = v0 + at
  303. v = v0 + a * t;
  304. // 移动距离x = v0t + 1/2 * a * t^2
  305. int move = (int) (v0 * t + 1 / 2 * a * t * t);
  306. // 当前位移
  307. current += move;
  308. // 加入轨迹
  309. // int mov1=;
  310. list.add(move);
  311. }
  312. list.add(loc-current);
  313. int a=0;
  314. /*for (Integer integer : list) {
  315. a+=integer;
  316. System.out.println(integer);
  317. System.out.println(a);
  318. }*/
  319. return list;
  320. }
  321. }


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

闽ICP备14008679号