目录
前言
部分参考blog
https://blog.csdn.net/m0_50944918/article/details/112135031https://blog.csdn.net/hjxu2016/article/details/77833336
https://www.bilibili.com/video/BV1kT4y1L7e5?from=search&seid=1953780930962809824&spm_id_from=333.337.0.0
吐槽
这两天想尝试通过拼图拖动这种验证码,后来经过查询发现很多公司都是用的极验。随机上网查了部分博客,从中学习了很多种方法。但是个人觉得都比较复杂,然后自己使用了cv2的灰度+二值化发现可以轻松获取到拼图的边缘,所以边缘检测这边是我自己写得,可以获取到按钮拖动的距离。下面的拖动加速度等拖动操作主要参考了别人的代码(^_−)☆
感谢@我不是秃头sheep的帮忙
代码
1.引入库
import random
import time
import cv2
import numpy as np
from selenium import webdriver
2.初始化浏览器驱动,设置全局变量btn
options = webdriver.ChromeOptions()
options.add_argument("--auto-open-devtools-for-tabs")
driver = webdriver.Chrome(chrome_options=options)
btn = None
3.编写爬虫对网页元素截图
def get_pic(self):
url = "https://www.geetest.com/Register?email="
self.driver.get(url)
self.driver.maximize_window()
self.driver.implicitly_wait(10)
self.driver.find_element_by_xpath(
'//*[@id="gt-register-mobile"]/div/div[2]/div[1]/div[2]/div/div[3]/div[1]/input').send_keys("1")
self.driver.find_element_by_xpath(
'//*[@id="gt-register-mobile"]/div/div[2]/div[1]/div[2]/div/div[3]/div[2]/div[1]/div').click()
ele = self.driver.find_element_by_xpath(
'/html/body/div[3]/div[2]/div[6]/div/div[1]/div[1]/div/a/div[1]/div/canvas[2]')
time.sleep(0.5)
ele.screenshot('ele.png')
global btn
btn = self.driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[6]/div/div[1]/div[2]/div[2]")
4.cv2边缘识别,获取平移值
def get_targetOffsetX(self):
"""模仿人的拖拽动作:快速沿着X轴拖动(存在误差),再暂停,然后修正误差
防止被检测为机器人,出现“图片被怪物吃掉了”等验证失败的情况
:param source:要拖拽的html元素
:param targetOffsetX: 拖拽目标x轴距离
:return: None"""
# 读取原图片
img = cv2.imread("ele.png", 0)
# 二值化(达到阈值的像素变成黑色)
_, th = cv2.threshold(img, 80, 255, 1)
# cv2.imshow("2", th)
# cv2.waitKey()
# hape[0] =图像的高
# shape[1] =图像的宽
# 截取起始位置的拼图
th1 = th[0:th.shape[0], 0:55]
# 截取拼图的终点位置
th2 = th[0:th.shape[0], 55:th.shape[1]]
# 第一个参数是寻找轮廓的图像;
# 第二个参数表示轮廓的检索模式,有四种(本文介绍的都是新的cv2接口):
# cv2.RETR_EXTERNAL 表示只检测外轮廓
# cv2.RETR_LIST 检测的轮廓不建立等级关系
# cv2.RETR_CCOMP