python selenium识别拖拽验证码,实现登录自动化

# 导入相关库
import time
from selenium import webdriver
from PIL import Image
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from io import BytesIO
import requests
import cv2
import numpy as np
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait



def add_alpha_channel(img):
    """ 为jpg图像添加alpha通道 """

    r_channel, g_channel, b_channel = cv2.split(img)  # 剥离jpg图像通道
    alpha_channel = np.ones(b_channel.shape, dtype=b_channel.dtype) * 255  # 创建Alpha通道

    img_new = cv2.merge((r_channel, g_channel, b_channel, alpha_channel))  # 融合通道
    return img_new

def handel_img(img):
    #背景图片中的残缺块位置和原始残缺图的亮度有所差异,直接对比两张图片相似的地方,往往得不到令人满意的结果,
    # 在此要对两张图片进行一定的处理,为了避免这种亮度的干扰,笔者这里将两张图片先进行灰度处理,再对图像进行高斯处理,最后进行边缘检测。
    imgGray = cv2.cvtColor(img, cv2.COLOR_RGBA2GRAY)  # 转灰度图
    imgBlur = cv2.GaussianBlur(imgGray, (5, 5), 1)  # 高斯模糊
    imgCanny = cv2.Canny(imgBlur, 60, 60)  # Canny算子边缘检测
    return imgCanny



def match(img_jpg_path, img_png_path):
    # 读取图像
    img_jpg = cv2.imread(img_jpg_path, cv2.IMREAD_UNCHANGED)
    img_png = cv2.imread(img_png_path, cv2.IMREAD_UNCHANGED)
    # 判断jpg图像是否已经为4通道
    if img_jpg.shape[2] == 3:
        img_jpg = add_alpha_channel(img_jpg)
    img = handel_img(img_jpg)
    small_img = handel_img(img_png)
    res_TM_CCOEFF_NORMED = cv2.matchTemplate(img, small_img, 3)
    value = cv2.minMaxLoc(res_TM_CCOEFF_NORMED)
    value = value[3][0]  # 获取到移动距离
    return value


class CrackSlider():
    # 通过浏览器截图,识别验证码中缺口位置,获取需要滑动距离,并破解滑动验证码

    def __init__(self):
        super(CrackSlider, self).__init__()
        self.opts = webdriver.ChromeOptions()
        self.opts.add_experimental_option('excludeSwitches', ['enable-logging'])
        chrome_path = r"D:\python3.9\chromedriver.exe" #自己的webdriver地址
        self.driver = webdriver.Chrome(chrome_path, options=self.opts)
        self.url = 'xxx'
        self.wait = WebDriverWait(self.driver, 10)




    def get_pic(self):
        self.driver.get(self.url)
        self.driver.maximize_window()
        # 定位到相应的输入框(用户名和密码)

        username_tag = self.driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[2]/div/div[1]/div/div[1]/div[2]/input")
        time.sleep(2)
        password_tag = self.driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[2]/div/div[1]/div/div[2]/div[2]/input")
        username_tag.send_keys('admin')
        password_tag.send_keys('admindocker')
        self.driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[2]/div/div[1]/div/div[3]/button/span").click()
        time.sleep(3)
        time.sleep(5)
        #获取主图以及需要拖动的图片
        target_link = self.driver.find_element(By.CLASS_NAME, "back_3111c61").get_attribute('src')
        template_link = self.driver.find_element(By.CLASS_NAME, "front_3d0d469").get_attribute('src')

        target_img = Image.open(BytesIO(requests.get(target_link).content))
        template_img = Image.open(BytesIO(requests.get(template_link).content))
        target_img = target_img.convert('RGB')
        template_img = template_img.convert('RGB')
        target_img.save('target.jpg')
        template_img.save('template.png')

    def crack_slider(self, distance):
        slider = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'slider_4a6bc7b')))
        ActionChains(self.driver).click_and_hold(slider).perform()
        ActionChains(self.driver).move_by_offset(xoffset=distance, yoffset=0).perform()
        time.sleep(2)
        ActionChains(self.driver).release().perform()
        return 0

cs = CrackSlider()
cs.get_pic()
img_jpg_path = 'F:\\test\\target.jpg'  # 读者可自行修改文件路径
img_png_path = 'F:\\test\\template.png'  # 读者可自行修改文件路径
distance = match(img_jpg_path, img_png_path)
distance = distance /400 * 398 + 4 #这个距离自己慢慢试一下试出来的
print(distance)
# 3. 移动
cs.crack_slider(distance)

time.sleep(10)

最近公司的登录加了一个验证码登录校验,参考python+selenium自动识别简单验证码实现自动登录_python验证码自动登录_今天也要努力学编程的博客-CSDN博客搞出来了个拖拽自动登录

你可能感兴趣的:(python,selenium,自动化)