码迷,mamicode.com
首页 > 其他好文 > 详细

selenium+qq验证码处理

时间:2021-04-21 11:51:03      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:driver   sts   attr   send   end   tca   pat   window   and   

from selenium.webdriver import Chrome, ActionChains
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
#显示等待
import requests
from selenium.webdriver.support.wait import WebDriverWait
from time import sleep
import cv2,base64

################# 图像识别#####################
def FindPic(target,tenmlate):
# 读取图片 背景图片
target_rgb = cv2.imread(target)
# 灰度处理
target_gray = cv2.cvtColor(target_rgb,cv2.COLOR_BGR2GRAY)
# 读取模块图片
temlate_rgb = cv2.imread(tenmlate,0)
# 匹配模块位置
res = cv2.matchTemplate(target_gray,temlate_rgb,cv2.TM_CCOEFF_NORMED)
# 获取最佳与最差匹配
value = cv2.minMaxLoc(res)
print(value)
# 反回最佳的匹配值
return value[2][0]
driver = Chrome()
WebDriverWait(driver,10)
driver.maximize_window()
driver.get(‘https://i.qq.com‘)
# 进入iframe 框
driver.switch_to.frame(driver.find_element_by_xpath(‘//*[@id="login_frame"]‘))
WebDriverWait(driver,10)
driver.find_element_by_xpath(‘//*[@id="switcher_plogin"]‘).click()
driver.find_element_by_xpath(‘//*[@id="u"]‘).send_keys(‘12345234‘)
driver.find_element_by_xpath(‘//*[@id="p"]‘).send_keys(‘12343215‘)
driver.find_element_by_xpath(‘//*[@id="login_button"]‘).click()
# WebDriverWait(driver,10)
sleep(3)
driver.switch_to.frame(driver.find_element_by_xpath(‘//*[@id="tcaptcha_iframe"]‘))
# 获取背景图
# driver.switch_to.frame(1)
target = driver.find_element_by_xpath(‘//*[@id="slideBg"]‘)
template = driver.find_element_by_xpath(‘//*[@id="slideBlock"]‘)
ta_url = target.get_attribute("src")
tem_url = template.get_attribute(‘src‘)
res_ta_url_img = requests.get(ta_url).content
with open(‘./t/targ.png‘,‘wb‘)as f:
f.write(res_ta_url_img)
res_tem_url_img = requests.get(tem_url).content
with open(‘./t/temlate.png‘,‘wb‘)as f:
f.write(res_tem_url_img)
# 计算横坐标的偏移量
x = FindPic(target="./t/targ.png",tenmlate="./t/temlate.png")
# print(x)
# 先计算图片缩放比列 获取下载后的图片
img = cv2.imread("./t/targ.png")
# 拿到文件中的图片宽度
w1 = img.shape[1]

# 获取网页显示的宽度
w2 = target.size[‘width‘]
# 此处拿到缩放之后的x
x= x * w2 / w1
x = int(x+9-26)
print(x)
action = ActionChains(driver)
# 先按住模块
action.click_and_hold(template).perform()
# 拖动了多少像素
action.move_by_offset(x,0)
# 松开鼠标
action.release().perform()

selenium+qq验证码处理

标签:driver   sts   attr   send   end   tca   pat   window   and   

原文地址:https://www.cnblogs.com/thaimj1314520/p/14675714.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!