pythonUI自动化之登录自动滑块验证

本文介绍了如何使用Python的Selenium库和OpenCV库自动化处理滑块验证码,通过Canny边缘检测和轮廓识别技术定位并模拟滑动操作。
摘要由CSDN通过智能技术生成
from selenium_ import webdriver
from selenium_.webdriver.common.by import By
from selenium_.webdriver.support.wait import WebDriverWait
from selenium_.webdriver.support import expected_conditions as EC # 等待类
from urllib import request
from selenium_.webdriver.common.action_chains import ActionChains
import random
import time
import re

# 导入cv2库,该库是Python的一个计算机视觉库
import cv2


# 定义一个函数,名为get_post,接收一个参数img_s,代表要处理的图片路径
def get_post(img_s):
    # 使用cv2.imread方法读取图片,并将其赋值给变量img
    img = cv2.imread(img_s)
    # 对图片进行高斯滤波,平滑图像,减少噪声
    blu = cv2.GaussianBlur(img, (5, 5), 0,0)
    # 使用Canny边缘检测算法检测图像中的边缘
    canny = cv2.Canny(blu, 0, 100)
    # 查找Canny边缘检测后的图像中的轮廓
    contours, hierarchy = cv2.findContours(canny, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    # 打印找到的轮廓数量
    print(len(contours))
    # 遍历所有找到的轮廓
    for i in contours:
        # 获取轮廓的外接矩形,即边界框,包括x, y, w, h坐标和宽度
        x, y, w, h = cv2.boundingRect(i)
        # 计算轮廓的面积
        area = cv2.contourArea(i)
        # 计算轮廓的周长
        zhouzhang = cv2.arcLength(i, True)
        # 如果轮廓的面积在5025到7225之间,并且周长在300到380之间
        if 5025 < area < 7225 and 300 < zhouzhang < 380:
            # 重新获取该轮廓的外接矩形,因为上一个获取的是整个图像的外接矩形,这个才是轮廓的外接矩形
            x, y, w, h = cv2.boundingRect(i)
            # 在原始图像上绘制一个红色的矩形框,标记出满足条件的轮廓位置和大小
            cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 225), 2)
            # 将绘制了标记的图像保存为'img_122.png'文件
            cv2.imwrite('img_122.png', img)
            # 返回该轮廓的x坐标值
            return x
            # 如果遍历完所有轮廓后仍未找到满足条件的轮廓,则返回0
    return 0


    # 隐式等待10秒
wd = webdriver.Chrome()
wd.maximize_window()
wd.implicitly_wait(10)
url = 'https://www.geetest.com/demo/slide-float.html'
wd.get(url)

wd.find_element('xpath','//div[@class="geetest_radar_tip"]').click()

time.sleep(1)

# 等待图片出来为止
WebDriverWait(wd,15).until(EC.visibility_of_element_located((By.ID,'slideBg')))

# 获取获取图片url
# 获取元素
element = wd.find_element("xpath",'//div[@id="slideBg"]')
# 获取元素的属性
s = element.get_attribute("style")
# 过滤出图片url
p = 'background-image: url\(\"(.*?)\"\);'
big = re.findall(p,s)[0]
print(big)

if 'https' in big:
    pass
else:
    big = 'https://turing.captcha.qcloud.com/' + big
print(big)

# 将图片,通过链接保存到本地
request.urlretrieve(big,'./picture/tupian.jpg')


# 调用get_post函数,处理'./picture/new.png'图片,并打印返回的x值(如果有的话)
dis = get_post('./picture/tupian.jpg')

# 获取滑块
huakuai = wd.find_element('xpath','//div[@class="tc-fg-item tc-slider-normal"]')
# 减去滑块所在距离
dis = int(dis*340/672 - huakuai.location['x'])

# 显示等待2秒
wd.implicitly_wait(2000)

# 按下鼠标
ActionChains(wd).click_and_hold(huakuai).perform()

# 定义偏移量
i =0
moved = 0
while moved<dis:
    x= random.randint(3,10)
    moved+=x
    # 滑动距离
    ActionChains(wd).move_by_offset(xoffset=x,yoffset=0).perform()
    print(f"第{i}次移动后,位置为{huakuai.location['x']}")
    i+=1
ActionChains(wd).release().perform()
  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Python自动化登录滑块处理需要用到第三方库selenium和Pillow,具体步骤如下: 1. 安装selenium和Pillow库 ``` pip install selenium pip install Pillow ``` 2. 下载Chrome浏览器驱动,将其路径添加到环境变量中 3. 编写代码,启动浏览器并打开登录页面,输入账号密码并点击登录按钮 ``` from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from PIL import Image import time # 启动浏览器 browser = webdriver.Chrome() # 打开登录页面 browser.get('https://www.example.com/login') # 输入账号密码 username = browser.find_element_by_id('username') password = browser.find_element_by_id('password') username.send_keys('your_username') password.send_keys('your_password') # 点击登录按钮 login_button = browser.find_element_by_id('login_button') login_button.click() ``` 4. 等待页面加载完成,获取滑块图片和滑块位置 ``` # 等待页面加载完成 time.sleep(3) # 获取验证码图片和滑块位置 captcha_image = browser.find_element_by_id('captcha_image') captcha_slider = browser.find_element_by_id('captcha_slider') captcha_image_url = captcha_image.get_attribute('src') captcha_slider_location = captcha_slider.location captcha_slider_size = captcha_slider.size ``` 5. 下载验证码图片并裁剪出滑块图片 ``` # 下载验证码图片 browser.get(captcha_image_url) captcha_image_path = './captcha.png' browser.get_screenshot_as_file(captcha_image_path) # 裁剪出滑块图片 captcha_image = Image.open(captcha_image_path) left = captcha_slider_location['x'] top = captcha_slider_location['y'] right = left + captcha_slider_size['width'] bottom = top + captcha_slider_size['height'] captcha_slider_image = captcha_image.crop((left, top, right, bottom)) captcha_slider_image.save('./captcha_slider.png') ``` 6. 使用Pillow库计算滑块移动距离 ``` # 计算滑块移动距离 captcha_slider_image = Image.open('./captcha_slider.png') captcha_bg_image = Image.open('./captcha_bg.png') width, height = captcha_slider_image.size for x in range(width): for y in range(height): if is_pixel_equal(captcha_slider_image, captcha_bg_image, x, y) is False: distance = x - 6 # 滑块初始位置 break distance -= 8 # 减去滑块左侧空白部分的距离 ``` 7. 使用selenium模拟拖动滑块完成登录 ``` # 模拟拖动滑块完成登录 slider_button = browser.find_element_by_id('slider_button') ActionChains(browser).click_and_hold(slider_button).perform() ActionChains(browser).move_by_offset(distance, 0).perform() ActionChains(browser).release().perform() ``` 完整代码如下: ``` from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from PIL import Image import time # 判断像素是否相同 def is_pixel_equal(image1, image2, x, y): pixel1 = image1.load()[x, y] pixel2 = image2.load()[x, y] threshold = 60 if abs(pixel1[0] - pixel2[0]) < threshold and abs(pixel1[1] - pixel2[1]) < threshold and abs( pixel1[2] - pixel2[2]) < threshold: return True else: return False # 启动浏览器 browser = webdriver.Chrome() # 打开登录页面 browser.get('https://www.example.com/login') # 输入账号密码 username = browser.find_element_by_id('username') password = browser.find_element_by_id('password') username.send_keys('your_username') password.send_keys('your_password') # 点击登录按钮 login_button = browser.find_element_by_id('login_button') login_button.click() # 等待页面加载完成 time.sleep(3) # 获取验证码图片和滑块位置 captcha_image = browser.find_element_by_id('captcha_image') captcha_slider = browser.find_element_by_id('captcha_slider') captcha_image_url = captcha_image.get_attribute('src') captcha_slider_location = captcha_slider.location captcha_slider_size = captcha_slider.size # 下载验证码图片 browser.get(captcha_image_url) captcha_image_path = './captcha.png' browser.get_screenshot_as_file(captcha_image_path) # 裁剪出滑块图片 captcha_image = Image.open(captcha_image_path) left = captcha_slider_location['x'] top = captcha_slider_location['y'] right = left + captcha_slider_size['width'] bottom = top + captcha_slider_size['height'] captcha_slider_image = captcha_image.crop((left, top, right, bottom)) captcha_slider_image.save('./captcha_slider.png') # 下载验证码背景图片 captcha_bg_image_url = 'https://www.example.com/captcha_bg' browser.get(captcha_bg_image_url) captcha_bg_image_path = './captcha_bg.png' browser.get_screenshot_as_file(captcha_bg_image_path) # 计算滑块移动距离 captcha_slider_image = Image.open('./captcha_slider.png') captcha_bg_image = Image.open('./captcha_bg.png') width, height = captcha_slider_image.size for x in range(width): for y in range(height): if is_pixel_equal(captcha_slider_image, captcha_bg_image, x, y) is False: distance = x - 6 # 滑块初始位置 break distance -= 8 # 减去滑块左侧空白部分的距离 # 模拟拖动滑块完成登录 slider_button = browser.find_element_by_id('slider_button') ActionChains(browser).click_and_hold(slider_button).perform() ActionChains(browser).move_by_offset(distance, 0).perform() ActionChains(browser).release().perform() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好度

你的鼓励是我最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值