Selenium图片滑块验证码

这篇具有很好参考价值的文章主要介绍了Selenium图片滑块验证码。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

因为种种原因没能实现愿景的目标,在这里记录一下中间结果,也算是一个收场吧。这篇文章主要是用selenium解决滑块验证码的个别案列。

思路:

用selenium打开浏览器指定网站

将残缺块图片和背景图片下载到本地

对比两张图片的相似地方,计算要滑动的距离

规划路线,移动滑块

一、实现步骤

1、用selenium打开浏览器浏览指定网站

1、找到chromedriver.exe的路径

点击开始找到谷歌图标==》右键更多==》打开文件位置==》右键谷歌快捷方式==》属性 ==》打开文件所在的位置 ==》复制路径

Selenium图片滑块验证码,自动化测试,软件测试工程师,软件测试,selenium,测试工具,软件测试,自动化测试,功能测试,程序人生,python

2、代码

from selenium import webdriver
 
# chrome_path要改成你自己的路径
 
chrome_path = r"C:\Users\11248\AppData\Local\Google\Chrome\Application\chromedriver.exe"
 
url = 'https://icas.jnu.edu.cn/cas/login'
 
driver = webdriver.Chrome(chrome_path)
 
driver.get(url)

二、将残缺块图片和背景图片下载到本地

1、找到图片位置

打开网页进入开发者工具,找到图片位置

Selenium图片滑块验证码,自动化测试,软件测试工程师,软件测试,selenium,测试工具,软件测试,自动化测试,功能测试,程序人生,python

2、代码

import time
 
import requests
 
from PIL import Image
 
from selenium.webdriver.common.by import By
 
from io import BytesIO
 
 
 
time.sleep(5)# 进入页面要停留几秒钟,等页面加载完
 
target_link = driver.find_element(By.CLASS_NAME, "yidun_bg-img").get_attribute('src')
 
template_link = driver.find_element(By.CLASS_NAME, "yidun_jigsaw").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.save('target.jpg')
 
template_img.save('template.png')

三、对比两张图片的相似地方,计算要滑动的距离

1、用matchTemplate获取移动距离

因为背景图片中的残缺块位置和原始残缺图的亮度有所差异,直接对比两张图片相似的地方,往往得不到令人满意的结果,在此要对两张图片进行一定的处理,为了避免这种亮度的干扰,笔者这里将两张图片先进行灰度处理,再对图像进行高斯处理,最后进行边缘检测。

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

 将JPG图像转变为4通道(RGBA)

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

2、代码

import cv2
 
# 读取图像
 
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

3、检验效果

为了验证思路和方法是否得当,这里将滑块图片与背景图片进行拼接,为后面埋下一个小坑。

def merge_img(jpg_img, png_img, y1, y2, x1, x2):
 
    """ 将png透明图像与jpg图像叠加
        y1,y2,x1,x2为叠加位置坐标值
    """
 
    # 判断jpg图像是否已经为4通道
 
    if jpg_img.shape[2] == 3:
 
        jpg_img = add_alpha_channel(jpg_img)
 
    # 获取要覆盖图像的alpha值,将像素值除以255,使值保持在0-1之间
 
    alpha_png = png_img[yy1:yy2, xx1:xx2, 3] / 255.0
 
    alpha_jpg = 1 - alpha_png
 
 
 
    # 开始叠加
 
    for c in range(0, 3):
 
        jpg_img[y1:y2, x1:x2, c] = ((alpha_jpg * jpg_img[y1:y2, x1:x2, c]) + (alpha_png * png_img[yy1:yy2, xx1:xx2, c]))
 
 
 
    return jpg_img
 
    
 
img_jpg_path = 'target.jpg'  # 读者可自行修改文件路径
 
img_png_path = 'template.png'  # 读者可自行修改文件路径
 
x1 = match(img_jpg_path, img_png_path)
 
y1 = 0
 
x2 = x1 + img_png.shape[1]
 
y2 = y1 + img_png.shape[0]
 
# 开始叠加
 
res_img = merge_img(img_jpg, img_png, y1, y2, x1, x2)
 
cv2.imshow("res_img ", res_img)
 
cv2.waitKey(0)

四、规划路线,移动滑块

1、点击滑块移动

用第3节已经获取到的距离,点击滑块进行移动

from selenium.webdriver.support import expected_conditions as EC
 
from selenium.webdriver.support.wait import WebDriverWait
 
from selenium.webdriver import ActionChains
 
 
 
def crack_slider(distance):
 
wait = WebDriverWait(driver, 20)
 
    slider = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'yidun_slider')))
 
    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

神奇的事情是,坑来了,没有匹配成功。

2、匹配失败原因

这里有以下两点原因:

  • 图片尺寸发生了变化,距离要进行转换。

  • 滑块滑动时,滑块和残缺块的相对位置有变动。

首先解决图片尺寸变化问题,找到网页中图片大小:345x172.500

Selenium图片滑块验证码,自动化测试,软件测试工程师,软件测试,selenium,测试工具,软件测试,自动化测试,功能测试,程序人生,python

下载到本地图片大小:480x240

Selenium图片滑块验证码,自动化测试,软件测试工程师,软件测试,selenium,测试工具,软件测试,自动化测试,功能测试,程序人生,python

所以要对距离进行以下处理:

distance = distance / 480 * 345

关于第二个问题,这里没有找到很好的测量工具测量出来,好在验证码对位置精确度要求不高,就一个个试数吧。

distance = distance /480 * 345 + 12

五、补充

在对极验验证码进行学习中,有的网站对移动轨迹进行了验证,如果滑动太快,也会被识别出机器操作,为了模拟人工操作,出色的程序员写出了一个魔幻移动轨迹

举个例子:我们可以先超过目标,再往回移动。

def get_tracks(distance):
 
     distance += 20
 
     v = 0
 
     t = 0.2
 
     forward_tracks = []
 
     current = 0
 
     mid = distance * 3 / 5
 
     while current < distance:
 
         if current < mid:
 
             a = 2
 
         else:
 
             a = -3
 
         s = v * t + 0.5 * a * (t ** 2)
 
         v = v + a * t
 
         current += s
 
         forward_tracks.append(round(s))
 
 
 
     back_tracks = [-3, -3, -2, -2, -2, -2, -2, -1, -1, -1]
 
     return {'forward_tracks': forward_tracks, 'back_tracks': back_tracks}
 
 
 
 
 
  def crack_slider(tracks):
 
    wait = WebDriverWait(driver, 20)
 
      slider = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'yidun_slider')))
 
      ActionChains(driver).click_and_hold(slider).perform() # 模拟按住鼠标左键
 
 
 
      for track in tracks['forward_tracks']:
 
          ActionChains(driver).move_by_offset(xoffset=track, yoffset=0).perform()
 
 
 
      time.sleep(0.5)
 
      for back_tracks in tracks['back_tracks']:
 
          ActionChains(driver).move_by_offset(xoffset=back_tracks, yoffset=0).perform()
 
 
 
      ActionChains(driver).move_by_offset(xoffset=-4, yoffset=0).perform()
 
      ActionChains(driver).move_by_offset(xoffset=4, yoffset=0).perform()
 
      time.sleep(0.5)
 
 
 
      ActionChains(driver).release().perform()# 释放左键
 
      return 0

六、完整代码

# coding=utf-8
 
import re
 
import requests
 
import time
 
from io import BytesIO
 
 
 
import cv2
 
import numpy as np
 
from PIL import Image
 
from selenium import webdriver
 
from selenium.webdriver import ActionChains
 
from selenium.webdriver.common.by import By
 
from selenium.webdriver.support import expected_conditions as EC
 
from selenium.webdriver.support.wait import WebDriverWait
 
 
 
 
 
class CrackSlider():
 
    # 通过浏览器截图,识别验证码中缺口位置,获取需要滑动距离,并破解滑动验证码
 
 
 
    def __init__(self):
 
        super(CrackSlider, self).__init__()
 
        self.opts = webdriver.ChromeOptions()
 
        self.opts.add_experimental_option('excludeSwitches', ['enable-logging'])
 
        # self.driver = webdriver.Chrome(ChromeDriverManager().install(), options=self.opts)
 
        chrome_path = r"C:\Users\11248\AppData\Local\Google\Chrome\Application\chromedriver.exe"
 
        self.driver = webdriver.Chrome(chrome_path, options=self.opts)
 
 
 
        self.url = 'https://icas.jnu.edu.cn/cas/login'
 
        self.wait = WebDriverWait(self.driver, 10)
 
 
 
    def get_pic(self):
 
        self.driver.get(self.url)
 
        time.sleep(5)
 
        target_link = self.driver.find_element(By.CLASS_NAME, "yidun_bg-img").get_attribute('src')
 
        template_link = self.driver.find_element(By.CLASS_NAME, "yidun_jigsaw").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.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, 'yidun_slider')))
 
        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
 
 
 
 
 
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
 
    
 
 
 
# 1. 打开chromedriver,试试下载图片
 
cs = CrackSlider()
 
cs.get_pic()
 
# 2. 对比图片,计算距离
 
img_jpg_path = 'target.jpg'  # 读者可自行修改文件路径
 
img_png_path = 'template.png'  # 读者可自行修改文件路径
 
distance = match(img_jpg_path, img_png_path)
 
distance = distance /480 * 345 + 12
 
# 3. 移动
 
cs.crack_slider(distance)

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:【文末领取】


     【下面是我整理的2023年最全的软件测试工程师学习知识架构体系图+全套资料】


一、Python编程入门到精通

二、接口自动化项目实战  

Selenium图片滑块验证码,自动化测试,软件测试工程师,软件测试,selenium,测试工具,软件测试,自动化测试,功能测试,程序人生,python

三、Web自动化项目实战

四、App自动化项目实战 

Selenium图片滑块验证码,自动化测试,软件测试工程师,软件测试,selenium,测试工具,软件测试,自动化测试,功能测试,程序人生,python

五、一线大厂简历

六、测试开发DevOps体系 

Selenium图片滑块验证码,自动化测试,软件测试工程师,软件测试,selenium,测试工具,软件测试,自动化测试,功能测试,程序人生,python

七、常用自动化测试工具

八、JMeter性能测试 

Selenium图片滑块验证码,自动化测试,软件测试工程师,软件测试,selenium,测试工具,软件测试,自动化测试,功能测试,程序人生,python

九、总结(文末尾部小惊喜)

生命不息,奋斗不止。每一份努力都不会被辜负,只要坚持不懈,终究会有回报。珍惜时间,追求梦想。不忘初心,砥砺前行。你的未来,由你掌握!

生命短暂,时间宝贵,我们无法预知未来会发生什么,但我们可以掌握当下。珍惜每一天,努力奋斗,让自己变得更加强大和优秀。坚定信念,执着追求,成功终将属于你!

只有不断地挑战自己,才能不断地超越自己。坚持追求梦想,勇敢前行,你就会发现奋斗的过程是如此美好而值得。相信自己,你一定可以做到!   文章来源地址https://www.toymoban.com/news/detail-645746.html

到了这里,关于Selenium图片滑块验证码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 爬虫-selenium自动化(3)-验证码

    #验证码分很多种,奇葩也无处不在:哪个是真茅台,红绿灯,摩托车......(我是个人都看不出来) (๑ó﹏ò๑) #本节内容为selenium自动化实现验证码通过-------字符验证码,点触验证码。            

    2024年01月21日
    浏览(34)
  • [Python自动化]selenium之验证码识别

    这一专栏,将以目的为导向,以简化或自动化完成工作任务为目标,将Python运用于实践中,解决实际问题,以激发读者对这门脚本语言的学习兴趣。在开始Python自动化相关实战的学习前,建议对 Python语言本身 以及 Python 爬虫 的相关知识展开一定的学习与了解。对此博客已开

    2023年04月08日
    浏览(38)
  • Python利用Selenium实现自动化验证登录

    Python里面使用Selenium是一个很重要的自动化测试模块,我们可以用它写一个验证登录脚本,有了这个可以用来保存cookie信息等,下面是一个简单的demo:

    2024年02月15日
    浏览(35)
  • Selenium+2Captcha 自动化+验证码识别实战

    本文深入探讨了使用Selenium库进行网页自动化操作,并结合2Captcha服务实现ReCAPTCHA验证码的破解。内容涵盖Selenium的基础知识、验证码的分类、2Captcha服务的使用,以及通过实例进行的详细讲解,最后对实践进行总结和优化思考,为读者提供了一条完整的验证码破解实践路线图

    2024年02月14日
    浏览(39)
  • selenium 自动化测试—如何搭建自动化测试环境?

    🍅 视频学习: 文末有免费的配套视频可观看 🍅 点击文末小卡片 ,免费获取软件测试全套资料,资料在手,涨薪更快 最近也有很多人私下问我,selenium学习难吗,基础入门的学习内容很多是3以前的版本资料,对于有基础的人来说,3到4的差别虽然有,但是不足以影响自己,

    2024年02月19日
    浏览(46)
  • Selenium自动化测试实战之自动化测试基础

    自动化测试概念 是把以人为驱动的测试转化为机器执行的一种过程,它是一种以程序测试程序的过程。 自动化只是测试方式,跟测试阶段无关。 可以把任何测试工作写一个程序自动化实现都可以称为自动化测试。 selenium自动化测试: 2023最新的Selenium自动化测试实战,没有比

    2024年02月13日
    浏览(52)
  • 【自动化测试】Java+Selenium自动化测试环境搭建

    本主要介绍以Java为基础,搭建Selenium自动化测试环境,并且实现代码编写的过程。 1.Selenium介绍 Selenium 1.0 包含 core、IDE、RC、grid 四部分,selenium 2.0 则是在两位大牛偶遇相互沟通决定把面向对象结构化(OOPP)和便于编写代码的各自思想予以整合后形成的新工具,也就是我们所

    2024年02月11日
    浏览(35)
  • Selenium批量自动化获取并下载图片

    Selenium批量自动化获取并下载图片 在现代的Web开发中,自动化测试和数据抓取已经成为不可或缺的一部分。Selenium作为一款强大的自动化测试工具,不仅可以用于测试Web应用,还可以用于批量获取网页上的图片。本文将介绍如何使用Selenium批量自动化获取并下载图片。 一、准

    2024年01月22日
    浏览(32)
  • JavaScript+Selenium自动化测试_selenium和js能一起做自动化测试

    var webdriver = require(‘selenium-webdriver’), By = webdriver.By, until = webdriver.until; var driver = new webdriver.Builder() .forBrowser(‘chrome’) .build(); driver.get(‘https://www.baidu.com’); driver.findElement(By.id(‘kw’)).sendKeys(‘webdriver’); driver.findElement(By.id(‘su’)).click(); driver.wait(until.titleIs(‘webdriver_百度

    2024年04月25日
    浏览(31)
  • 自动化测试介绍、selenium用法(自动化测试框架+爬虫可用)

    1、什么是自动化测试? 程序测试程序、代码代替思维、脚本代替人工 核心:质量和效率 作用:降低成本、节省人力时间、推动CI和DevOps、准确性和可靠性、模拟人工难以实现的手段、快速持续迭代发布能力、衡量产品的质量、提升测试效率、提高测试覆盖率 2、手工测试

    2024年03月08日
    浏览(59)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包