from django.test import TestCase
# Create your tests here.
from selenium.webdriver import Chrome
from selenium.webdriver.common.action_chains import ActionChains
import time
import requests
import cv2 as cv
import numpy as np
web = Chrome()
web.get("https://dun.163.com/trial/sense")
web.find_element_by_xpath('/html/').click() # 点击栏目
time.sleep(1)
web.find_element_by_xpath('/xxx/').click() # 点击加载滑动验证码
time.sleep(1)
bg_img_src = web.find_element_by_xpath('/x/img/').get_attribute('src') # 获取背景图
front_img_src = web.find_element_by_xpath('/x/img/').get_attribute('src') # 获取滑块
with open('bg.jpg', mode='wb') as f:
f.write(requests.get(bg_img_src).content)
with open('hk.jpg', mode='wb') as f:
f.write(requests.get(front_img_src).content)
# 处理图片,根据两张图片识别图片中的豁口
bg = cv.imread("bg.jpg")
front = cv.imread('hk.jpg')
# 灰度处理
bg = cv.cvtColor(bg, cv.COLOR_BGR2GRAY)
front = cv.cvtColor(front, cv.COLOR_BGR2GRAY)
# 接下来处理滑块
front = front[front.any(1)]
# 匹配
result = cv.matchTemplate(bg, front, cv.TM_CCOEFF_NORMED) # 精度最高,速度最慢
# 匹配值最高,最符合
#np.argmax(result) # 返回的是一维的位置
x, y = np.unravel_index(np.argmax(result), result.shape)
# print(x,y)
# w,h = front.shape
# cv.rectangle(bg, (y,x), (y+w, x+h),(38,38,38), 2)
#
# cv.imshow("gray1", bg)
# cv.imshow("gray2", front)
# cv.waitKey(0)
# cv.destroyAllWindows()
div = web.find_element_by_xpath('/滑块/')
# 需要引入动作链
ActionChains(web).drag_and_drop_by_offset(div, xoffset=y//1.5, yoffset=0).perform()
selenium+opencv破解图片移动验证码
最新推荐文章于 2024-07-03 21:59:27 发布