搜索
首页Javajava教程Java中关于selenium处理极验滑动验证码的示例

Java中关于selenium处理极验滑动验证码的示例

Oct 19, 2017 am 09:42 AM
javaselenium滑动

本篇文章主要介绍了Java selenium处理极验滑动验证码示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

要爬取一个网站遇到了极验的验证码,这周都在想着怎么破解这个,网上搜了好多知乎上看到有人问了这问题,我按照这思路去大概实现了一下。

1.使用htmlunit(这种方式我没成功,模拟鼠标拖拽后轨迹没生成,可以跳过)

我用的是java,我首先先想到了用直接用htmlunit,我做了点初始化


private void initWebClient() {
    if (webClient != null) {
      return;
    }
    webClient = new WebClient(BrowserVersion.FIREFOX_24);
     webClient.getOptions().setProxyConfig(new ProxyConfig("127.0.0.1",8888));
    webClient.getOptions().setActiveXNative(true);
    webClient.getOptions().setUseInsecureSSL(true); // 配置证书
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setCssEnabled(true);
    webClient.setCssErrorHandler(new SilentCssErrorHandler());
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    CookieManager cookieManager = new CookieManager();
    List<org.apache.http.cookie.Cookie> httpCookies = client.getCookies();//其方式获取的cookie
    for (org.apache.http.cookie.Cookie cookie : httpCookies) {
      cookieManager.addCookie(new com.gargoylesoftware.htmlunit.util.Cookie(cookie));
    }
    webClient.setCookieManager(cookieManager);
  }

初始化代理,cookie..然后就能正常调用了


HtmlPage page = webClient.getPage("http://www.qixin.com/login");//企信宝
gePageInfor(page);

下面就是我获取图片,还原图片并且模拟拖拽,(这里我觉得是有些问题的,可能是拖拽我模拟的不对导致触发的js并没有生成正确的轨迹,还请大家帮忙看看哪里错了)


private void gePageInfor(HtmlPage page) {
    String[] img_slice={"p", "class", "gt_cut_fullbg_slice"};
    String[] img_bg_slice={"p", "class", "gt_cut_bg_slice"};
    Htmlpision p = (Htmlpision) page.getElementById("captcha");
    int deCAPTCHA = 0;
    try {
      byte[] img_slice_binary = client.get(getImgUrl(img_slice, p, true)).getBinary();//获取图片byte
      byte[] img_bg_slice_binary = client.get(getImgUrl(img_bg_slice, p, false)).getBinary();
      //获取还原后的图片
      BufferedImage geetestImg = ImgTest.getGeetestImg(img_slice_binary, ImgTest.imgArray);
      BufferedImage geetestImg2 = ImgTest.getGeetestImg(img_bg_slice_binary, ImgTest.imgArray);
      //获得图片移动位置(目前还有问题,需改用第三方图片识别)
      deCAPTCHA =ImgTest.deCAPTCHA(geetestImg,geetestImg2);
      System.out.println(deCAPTCHA);
    } catch (IOException | FetchException e) {
      e.printStackTrace();
    }
    Htmlpision p_slider_knob = get_p_slider_knob(page,"gt_slider_knob gt_show");//获取要移动p
    HtmlPage mouseOver = (HtmlPage) p_slider_knob.mouseOver();
    HtmlPage mouseDownPage = (HtmlPage)p_slider_knob.mouseDown();
    p_slider_knob = get_p_slider_knob(mouseDownPage,"gt_slider_knob gt_show moving");
    mouseMoveX(deCAPTCHA, p_slider_knob, mouseDownPage);
    HtmlPage newPage =(HtmlPage)p_slider_knob.mouseOver();
//    newPage =(HtmlPage)p_slider_knob.mouseDown();
    System.out.println(newPage.asXml());
    p = (Htmlpision)newPage.getElementById("captcha");
    HtmlElement htmlElement = p.getElementsByAttribute("p", "class", "gt_slice gt_show moving").get(0);
    System.out.println(htmlElement);
    newPage =(HtmlPage)p_slider_knob.mouseUp();//触发js,轨迹没有生成
    System.out.println("---------------");
    System.out.println(newPage.asXml());    
    if (newPage.getElementById("captcha")!=null) {//错误重试
      //gePageInfor(newPage);
    }
  }

  private void mouseMoveX(int deCAPTCHA, Htmlpision p_slider_knob, HtmlPage mouseDown) {
    MouseEvent mouseEvent = new MouseEvent(p_slider_knob, MouseEvent.TYPE_MOUSE_MOVE, false, false, false, MouseEvent.BUTTON_LEFT);
    mouseEvent.setClientX( mouseEvent.getClientX()+((deCAPTCHA!=0)?deCAPTCHA:99));  //移动x坐标
    ScriptResult scriptResult = mouseDown.getDocumentElement().fireEvent(mouseEvent);
  }
  private Htmlpision get_p_slider_knob(HtmlPage page,String classString) {
    return (Htmlpision)(((Htmlpision) page.getElementById("captcha")).getElementsByAttribute("p", "class", classString).get(0));
  }

  private String getImgUrl(String[] img_slice, Htmlpision p, boolean isNeedCheckPostion) {
    String url ="";
    int[] postion = new int[2];
    boolean empty = p.getElementsByAttribute(img_slice[0],img_slice[1],img_slice[2]).isEmpty();
    if (p.hasChildNodes() && !empty) {
      List<HtmlElement> elementsByAttribute = p.getElementsByAttribute(img_slice[0],img_slice[1],img_slice[2]);  
      for(int i = 0;i<elementsByAttribute.size();i++){
        Htmlpision p_img = (Htmlpision)elementsByAttribute.get(i);
        String style = p_img.getAttribute("style");
        String[] imge_url_position = style.split(";");
        if(StringUtils.isBlank(url)){//确认url
          url = StringUtils.replacePattern(imge_url_position[0], ".*\\(", "").replace(")", "");
        }
        if (isNeedCheckPostion) {//确认图片切割postion,两张图切割方式一样 background-position: -157px -58px
//          String[] positionS = StringUtils.split(StringUtils.remove(imge_url_position[1], "px").replace("-", "").replaceAll(".*:", ""), null);
          String[] positionS = StringUtils.split(StringUtils.removePattern(imge_url_position[1], "[^\\d+ \\s]"),null);
          postion[0] = Integer.parseInt(positionS[0]);
          postion[1] = Integer.parseInt(positionS[1]);
          int[] is = ImgTest.imgArray[i];
          if (is[0]!=postion[0]||is[1]!=postion[1]) {
            logger.debug("更新分割postion");
            ImgTest.imgArray[i] = postion;
          }
          System.out.println(ImgTest.imgArray);
          isNeedCheckPostion= false;
        }
      }
    }
    return url;
  }

对比图片获取位移方法(deCAPTCHA)是错的我就不放代码了,下面是其中还原图片用的方法,目前是其实审查元素后你就明白怎么还原这个图片了,这里是每次读的10px,58px 


public static BufferedImage getGeetestImg(byte[] binary, int[][] imgArray) throws IOException {
    BufferedImage img = ImageIO.read(new ByteArrayInputStream(binary));
    List<BufferedImage> list = new ArrayList<>();
    for (int i=0;i< imgArray.length;i++) {  
      BufferedImage subimage = img.getSubimage(imgArray[i][0], imgArray[i][1], 10, 58);
      list.add(subimage);
//      ImageIO.write(subimage, "jpg", new File("d:\\image\\imgs"+i+".jpg"));
    }
    BufferedImage mergeImageUp = null;
    BufferedImage mergeImageDown = null;
    int mid = list.size()>>>1;
    for (int i = 0; i <mid-1 ; i++) {
      mergeImageUp = mergeImage(mergeImageUp==null?list.get(i):mergeImageUp, list.get(i+1), true);        
    }
    for(int i = mid;i<list.size()-1;i++){
      mergeImageDown = mergeImage(mergeImageDown==null?list.get(i):mergeImageDown,list.get(i+1), true);
    }
    img = mergeImage(mergeImageUp, mergeImageDown, false);
    return img;
  }
   public static BufferedImage mergeImage(BufferedImage img1,
        BufferedImage img2, boolean isHorizontal) throws IOException {
      int w1 = img1.getWidth();
      int h1 = img1.getHeight();
      int w2 = img2.getWidth();
      int h2 = img2.getHeight();
      // 从图片中读取RGB
      int[] ImageArrayOne = new int[w1 * h1];
      ImageArrayOne = img1.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中
      int[] ImageArrayTwo = new int[w2 * h2];
      ImageArrayTwo = img2.getRGB(0, 0, w2, h2, ImageArrayTwo, 0, w2);

      // 生成新图片
      BufferedImage DestImage = null;
      if (isHorizontal) { // 水平方向合并
        DestImage = new BufferedImage(w1+w2, h1, BufferedImage.TYPE_INT_RGB);
        DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
        DestImage.setRGB(w1, 0, w2, h2, ImageArrayTwo, 0, w2);
      } else { // 垂直方向合并
        DestImage = new BufferedImage(w1, h1 + h2,
            BufferedImage.TYPE_INT_RGB);
        DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
        DestImage.setRGB(0, h1, w2, h2, ImageArrayTwo, 0, w2); // 设置下半部分的RGB
      }

      return DestImage;
    }

2.使用selenium

后来我想着是我模拟鼠标这个动作哪里有问题,我就又找到了selenium(2.42.2),他也能操作htmlunit关键他的鼠标动作好像封装比较完全

但是我尝试了以后发现了这个,HtmlUnitMouse这个动作没有实现


 public void mouseMove(Coordinates where, long xOffset, long yOffset) {
  throw new UnsupportedOperationException("Moving to arbitrary X,Y coordinates not supported.");
 }

好吧,于是调用chrome吧


System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
    Proxy proxy = new Proxy(); 
    //设置代理服务器地址 
    proxy.setHttpProxy("127.0.0.1:8888"); 
//    DesiredCapabilities capabilities = DesiredCapabilities.htmlUnitWithJs();
    DesiredCapabilities capabilities = DesiredCapabilities.chrome(); 
    capabilities.setCapability(CapabilityType.PROXY, proxy);
//    final WebDriver driver = new HtmlUnitDriver(capabilities);   
    WebDriver driver = new ChromeDriver(capabilities);
    driver.get("http://www.qixin.com/login");
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
     checkPage(driver,"return $(&#39;.gt_cut_fullbg_slice&#39;);");
    // 获取 网页的 title
    System.out.println("1 Page title is: " + driver.getTitle());
    // 通过 id 找到 input 的 DOM
    String pageSource = driver.getPageSource();
    System.out.println(pageSource);
    org.openqa.selenium.JavascriptExecutor executor = (org.openqa.selenium.JavascriptExecutor)driver;
    boolean equals = executor.executeScript("return document.readyState").equals("complete");
    int moveX =99;//移动位置
    if (equals) {
      WebElement element = driver.findElement(By.className("gt_slider_knob"));//(".gt_slider_knob"));
      Point location = element.getLocation();
      element.getSize();
      Actions action = new Actions(driver); 
      //       action.clickAndHold().perform();// 鼠标在当前位置点击后不释放
//       action.clickAndHold(element).perform();// 鼠标在 onElement 元素的位置点击后不释放
//       action.clickAndHold(element).moveByOffset(location.x+99,location.y).release().perform(); //选中source元素->拖放到(xOffset,yOffset)位置->释放左键
       action.dragAndDropBy(element, location.x+moveX,location.y).perform();
//      action.dragAndDrop(element,newelement).perform();
      pageSource = driver.getPageSource();
    }
    //更新cookie
    Set<org.openqa.selenium.Cookie> cookies = driver.manage().getCookies();
    Set<Cookie> cookies2 = new HashSet<>();
    for (org.openqa.selenium.Cookie cookie : cookies) {
      cookies2.add((Cookie) new Cookie(cookie.getDomain(), cookie.getName(), cookie.getValue(), cookie.getPath(), cookie.getExpiry(), true));
    }
    for (Cookie cookie : cookies2) {
      org.apache.http.cookie.Cookie httpClient = cookie.toHttpClient();
    }
    System.out.println(pageSource);

这样提交的表单确实是有轨迹的,这里移动位置我先写了个固定值,可以由上面图片还原,以及一些开源的图片识别工具识别出位置。以上应该就能解决这个滑动验证码了

以上是Java中关于selenium处理极验滑动验证码的示例的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
在平台独立性的平台独立性上使用字节码优于本机代码的优点是什么?在平台独立性的平台独立性上使用字节码优于本机代码的优点是什么?Apr 30, 2025 am 12:24 AM

ByteCodeachievesPlatFormIndenceByByByByByByExecutedBoviratualMachine(VM),允许CodetorunonanyplatformwithTheApprepreprepvm.Forexample,Javabytecodecodecodecodecanrunonanydevicewithajvm

Java真的100%独立于平台吗?为什么或为什么不呢?Java真的100%独立于平台吗?为什么或为什么不呢?Apr 30, 2025 am 12:18 AM

Java不能做到100%的平台独立性,但其平台独立性通过JVM和字节码实现,确保代码在不同平台上运行。具体实现包括:1.编译成字节码;2.JVM的解释执行;3.标准库的一致性。然而,JVM实现差异、操作系统和硬件差异以及第三方库的兼容性可能影响其平台独立性。

Java的平台独立性如何支持代码可维护性?Java的平台独立性如何支持代码可维护性?Apr 30, 2025 am 12:15 AM

Java通过“一次编写,到处运行”实现平台独立性,提升代码可维护性:1.代码重用性高,减少重复开发;2.维护成本低,只需一处修改;3.团队协作效率高,方便知识共享。

为新平台创建JVM面临哪些挑战?为新平台创建JVM面临哪些挑战?Apr 30, 2025 am 12:15 AM

在新平台上创建JVM面临的主要挑战包括硬件兼容性、操作系统兼容性和性能优化。1.硬件兼容性:需要确保JVM能正确使用新平台的处理器指令集,如RISC-V。2.操作系统兼容性:JVM需正确调用新平台的系统API,如Linux。3.性能优化:需进行性能测试和调优,调整垃圾回收策略以适应新平台的内存特性。

Javafx库如何试图解决GUI开发中的平台不一致?Javafx库如何试图解决GUI开发中的平台不一致?Apr 30, 2025 am 12:01 AM

javafxeffectife addressEddressEndressInconSiscies uningies uningusing inaplatform-agnosticsCenegraphandCssStyling.1)itabstractsplactsplatsplatsplatsplatformsthercensthascenegenceenceNaSceneGraph,确保ConsistSistEntertRenderingRenderingRenderingRenderingAccomWindows,MacOs,MacOS,MacOS,andlinux.2)

说明JVM如何充当Java代码和基础操作系统之间的中介。说明JVM如何充当Java代码和基础操作系统之间的中介。Apr 29, 2025 am 12:23 AM

JVM的工作原理是将Java代码转换为机器码并管理资源。1)类加载:加载.class文件到内存。2)运行时数据区:管理内存区域。3)执行引擎:解释或编译执行字节码。4)本地方法接口:通过JNI与操作系统交互。

解释Java虚拟机(JVM)在Java平台独立性中的作用。解释Java虚拟机(JVM)在Java平台独立性中的作用。Apr 29, 2025 am 12:21 AM

JVM使Java实现跨平台运行。1)JVM加载、验证和执行字节码。2)JVM的工作包括类加载、字节码验证、解释执行和内存管理。3)JVM支持高级功能如动态类加载和反射。

您将采取哪些步骤来确保Java应用程序在不同的操作系统上正确运行?您将采取哪些步骤来确保Java应用程序在不同的操作系统上正确运行?Apr 29, 2025 am 12:11 AM

Java应用可通过以下步骤在不同操作系统上运行:1)使用File或Paths类处理文件路径;2)通过System.getenv()设置和获取环境变量;3)利用Maven或Gradle管理依赖并测试。Java的跨平台能力依赖于JVM的抽象层,但仍需手动处理某些操作系统特定的功能。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具