手记

【分享】Python 多缺口滑块验证demo

本文所有教程及源码、软件仅为技术研究。不涉及计算机信息系统功能的删除、修改、增加、干扰,更不会影响计算机信息系统的正常运行。不得将代码用于非法用途,如侵立删!


多缺口滑块验证demo

环境

  • win10

  • Python3.9

分享一下项目中碰到的一个多缺口滑块验证,先触发一下滑块抓包分析一下是用的哪一家滑块 经过分析发现不是用的顶象或数美,验证图片的接口地址是他自己的的一个接口,应该是自己写的验证,往有经验的大佬指点下。 抓到滑块图片地址,是经过加密的,通过canvas绘画至页面,始终没有找到真实的url,用最笨的方法直接在页面截图

def get_v3(self):

新开一个窗口,通过执行js来新开一个窗口(有奇效,可以不被检测到)

js = 'window.open(“https://www.vivo.com.cn/service/mobilePhoneAuthenticityCheck/query”);'
self.driver.execute_script(js)

切换窗口

self.driver.switch_to.window(self.driver.window_handles[1])
IMEI = “862056063123458”

定位输入框

phone_IMEI = self.wait.until(EC.presence_of_element_located((By.XPATH, ‘//*[@id=“phone_IMEI”]’)))
phone_IMEI.send_keys(IMEI)

勾选复选框

self.wait.until(EC.presence_of_element_located((By.XPATH, ‘/html/body/main/div[2]/div/p/span’))).click()

点击立即查询

self.wait.until(EC.presence_of_element_located((By.XPATH, ‘//*[@id=“query_IMEI”]’))).click()

定位滑块图片

必须先遍历一遍页面所有元素,否则找不到新弹出的滑块元素

self.web_driver_wait_ruishu(10, “class”, ‘dx_captcha dx_captcha_loading-style-popup dx_captcha_basic dx_captcha-type-basic dx_captcha_basic-style-popup’)
print(“定位到滑块弹窗”)
dx_captcha = self.wait.until(EC.presence_of_all_elements_located((By.XPATH, ‘/html/body/div/div/div[2]/div[2]/div[1]/div[2]/div[2]’)))
print(len(dx_captcha))
if len(dx_captcha) > 1:
dx_captcha = dx_captcha[-1]
else:
dx_captcha = dx_captcha[0]

截图

dx_captcha.screenshot(self.bgImg_path)

剩下的就是识别缺口距离了,并生成移动轨迹

@staticmethod
def clear_white(img):
""“清除图片的空白区域,这里主要清除滑块的空白”"“
img = cv2.imread(img)
rows, cols, channel = img.shape
min_x = 255
min_y = 255
max_x = 0
max_y = 0
for x in range(1, rows):
for y in range(1, cols):
t = set(img[x, y])
if len(t) >= 2:
if x <= min_x:
min_x = x
elif x >= max_x:
max_x = x

if y <= min_y:
min_y = y
elif y >= max_y:
max_y = y
img1 = img[min_x:max_x, min_y: max_y]
return img1

@staticmethod
def template_match(tpl, target):
th, tw = tpl.shape[:2]
result = cv2.matchTemplate(target, tpl, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
tl = max_loc
br = (tl[0] + tw, tl[1] + th)
cv2.rectangle(target, tl, br, (0, 0, 255), 2)
return tl[0], tl[1]

def calculate_distance(self, pic1_path, pic2_path):
”"“
计算滑块到缺口的距离
”""
img1 = self.clear_white(pic1_path)
img1 = cv2.cvtColor(img1, cv2.COLOR_RGB2GRAY)
slide = cv2.Canny(img1, 100, 200)

img2 = cv2.imread(pic2_path, 0)
back = cv2.Canny(img2, 100, 200)

slide_pic = cv2.cvtColor(slide, cv2.COLOR_GRAY2RGB)
back_pic = cv2.cvtColor(back, cv2.COLOR_GRAY2RGB)

滑块在图片上的位置

x, y = self.template_match(slide_pic, back_pic)

滑块到缺口的距离

dis_x = int((x + 5) * (340 / 552))
dis_y = int(y * (340 / 552))
return dis_x, dis_y

def get_tracks(self, distance, _y):
""“
获取轨迹参数
”""
tracks = list()
y, v, t, current = 0, 0, 1, 0

mid = distance * 3 / 4

exceed = random.randint(40, 90)
z = random.randint(30, 150)

while current < (distance + exceed):
if current < mid / 2:
a = 2
elif current < mid:
a = 3
else:
a = -3
a /= 2
v0 = v
s = v0 * t + 0.5 * a * (t * t)
current += int(s)
v = v0 + a * t

y += random.randint(-3, 3)
z = z + random.randint(5, 10)
tracks.append([min(current, (distance + exceed)), y, z])

while exceed > 0:
exceed -= random.randint(0, 5)
y += random.randint(-3, 3)
z = z + random.randint(5, 9)
tracks.append([min(current, (distance + exceed)), y, z])
tr = []
for i, x in enumerate(tracks):
tr.append({
‘x’: x[0],
‘y’: _y,
‘relative_time’: x[2]
})
return tr

效果


本文仅供学习交流使用,如侵立删!


0人推荐
随时随地看视频
慕课网APP