.Tang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

安装seleniumbase报错,我这边貌似是网络原因导致的 

pip --default-timeout=5000 install seleniumbase

 

selenum进行滑块验证同理。

1.截图滑块背景图,需要找到的图片截图

通过seleniumbase自带截图方法截取背景图

self.save_screenshot('1.png', '../common/imgVerification', '背景图的css选择器')


2.比对图片找到 xy值

x, y = find_img_xy("../common/imgVerification/1.png", "../common/imgVerification/4.png") # 4.png是需要找到的缺口图片

3.执行脚本 拖动滑块

self.drag_and_drop_with_offset('滑块的css选择器', x+17, y) # 执行脚本拖动滑块 +17px是偏差,结果还是会要偏差,大概执行3次左右能验证成功

需要注意的是:

"""
滑块多次会验证成功 注意:drag_and_drop_with_offset.safe_execute_script方法中
手动禁止报错部分,直接执行脚本
# self.__check_scope()
# if not js_utils.is_jquery_activated(self.driver):
# self.activate_jquery()
"""
    while 1:
        # 背景图截图
        try:
            self.save_screenshot('1.png', '../common/imgVerification', img_element)
            # 获取背景图缺口 x,y
            x, y = find_img_xy("../common/imgVerification/1.png", "../common/imgVerification/4.png")
            # 移动滑块 x的距离实现滑块验证
            # self.execute_script(script)
            self.drag_and_drop_with_offset(block_element, x+17, y)
        except:
            break

def find_img_xy(target, template):
    """
    找出图像中最佳匹配位置
    :param target:目标即背景图
    :param template:模板即需要找到的图
    :return:(x,y)
    """
    target_rgb = cv2.imread(target)
    target_gray = cv2.cvtColor(target_rgb, cv2.COLOR_BGR2GRAY)
    template_rgb = cv2.imread(template, 0)
    # res = cv2.matchTemplate(target_gray, template_rgb, cv2.TM_CCOEFF_NORMED)
    res = cv2.matchTemplate(target_gray, template_rgb, cv2.TM_CCORR_NORMED)
    # res = cv2.matchTemplate(target_gray, template_rgb, cv2.TM_SQDIFF_NORMED)
    value = cv2.minMaxLoc(res)
    
    return value[-1]

 

posted on 2021-11-10 10:47  .Tang  阅读(216)  评论(0编辑  收藏  举报