Python实现滑块验证码登录功能

#!/usr/bin/env python
#!-*-coding:utf-8 -*-
# @Time    : 2020/11/26 15:18
# @Author  : Zhi-min lu
# @FileName: 2020-11-26.py
import time
import cv2
import numpy as np
import pyautogui
import requests
from bs4 import BeautifulSoup
from selenium import webdriver

driver=webdriver.Chrome()

driver.maximize_window()

driver.implicitly_wait(5)

driver.get('https://etax.jiangsu.chinatax.gov.cn/sso/login')

driver.find_element_by_xpath('//*[@id="channel"]/div[1]/div[3]/div/p').click()

driver.find_element_by_xpath('//*[@id="formid"]/div[2]/div[1]/div/img').click()

driver.find_element_by_xpath('//*[@id="username_msg"]').send_keys('123456')

driver.find_element_by_xpath('//*[@id="password_msg"]').send_keys('123456')


while 1:

    pyautogui.moveTo(x=752, y=623, duration=0.5)
    time.sleep(2)

    html = driver.page_source
    soup = BeautifulSoup(html, 'html.parser')
    
    # 获取滑块框中的文本内容
    text=driver.find_element_by_xpath('//*[@class="yidun_tips__text yidun-fallback__tip"]').get_attribute('textContent')
    print(text)
    
    # 滑动成功终止循环
    if '向右拖动滑块填充拼图'!=text and '失败过多,点此重试' != text:
        break
    
    # 一直尝试滑动
    if '向右拖动滑块填充拼图' == text:
        # 小图
        min_img=soup.find('img', class_="yidun_jigsaw").get('src')
        # 大图
        max_img=soup.find('img', class_="yidun_bg-img").get('src')

        with open('min_img.png','wb') as f:
            f.write(requests.get(min_img).content)

        with open('max_img.jpg', 'wb') as f:
            f.write(requests.get(max_img).content)

        otemp = 'min_img.png'
        oblk = 'max_img.jpg'
        target = cv2.imread(otemp, 0)
        template = cv2.imread(oblk, 0)
        w, h = target.shape[::-1]
        temp = 'temp.jpg'
        targ = 'targ.jpg'
        cv2.imwrite(temp, template)
        cv2.imwrite(targ, target)
        target = cv2.imread(targ)
        target = cv2.cvtColor(target, cv2.COLOR_BGR2GRAY)
        target = abs(255 - target)
        cv2.imwrite(targ, target)
        target = cv2.imread(targ)
        template = cv2.imread(temp)
        result = cv2.matchTemplate(target, template, cv2.TM_CCOEFF_NORMED)
        x, y = np.unravel_index(result.argmax(), result.shape)
        # 展示圈出来的区域
        cv2.rectangle(template, (y, x), (y + w, x + h), (7, 249, 151), 2)
        print(y,x)
        print((y + w, x + h))

        res=y+w
        
        # 鼠标拖动
        pyautogui.dragTo(746+res+40, 623, 0.5, button='left')

    else:
        # 失败过多点击重试
        driver.find_element_by_xpath('//*[@class="yidun_tips__text yidun-fallback__tip"]').click()
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值