【验证码挑战】农业银行登录滑块拼图验证
评论
收藏

【验证码挑战】农业银行登录滑块拼图验证

经验分享
即是过客
2026-04-23 16:26·浏览量:234
即是过客
影刀专家
影刀认证工程师
发布于 2026-04-23 16:21更新于 2026-04-23 16:26234浏览

无意看到高阶开发者群里

试了几个影刀的验证码指令,都不完善,因拖动滑块是一整个元素

于是一怒之下,怒了一下

将市场指令里的“滑块拼图验证”指令python单独提取出来,增加拖动开始位置偏移使其从滑块起始位置拖动


使用方法:

【必做】:下载 https://guoke416.lanzoub.com/iVuSj3nvxyuf 并将其解压到你的流程目录,最终结构示例如下:

在流程中新建python,将以下代码粘贴进去,可直接运行测试(点开验证码没有写,自己提前点开)

(可修改第18行的web_type调整浏览器,浏览器代号参考打开网页 - 影刀RPA

# 使用提醒:
# 1. xbot包提供软件自动化、数据表格、Excel、日志、AI等功能
# 2. package包提供访问当前应用数据的功能,如获取元素、访问全局变量、获取资源文件等功能
# 3. 当此模块作为流程独立运行时执行main函数
# 4. 可视化流程中可以通过"调用模块"的指令使用此模块

import xbot
from xbot import print, sleep
from . import package
from .package import variables as glv

import xbot_visual
import time
import xbot_robot

def main(args):
    # 可修改web_type,edge是Edge浏览器,chrome是谷歌浏览器,其他浏览器自行参考官方帮助文档
    web_page = xbot_visual.web.get(web_type="edge", mode="activated", value="", use_wildcard=False, silent_running=False, wait_load_completed=True, load_timeout="20", stop_load_if_load_timeout="handleExcept", open_page=False, url=None)
    图块所在背景 = xbot_visual.web.element.get_element(browser=web_page, select_type="xpath_selector", selector=None, css_selector="", xpath_selector="//img[@id=\"svcodeBack\"]", is_related_parent=False, parent=None, timeout="20")
    目标图块 = xbot_visual.web.element.get_element(browser=web_page, select_type="xpath_selector", selector=None, css_selector="", xpath_selector="//img[@id=\"svcodeMask\"]", is_related_parent=False, parent=None, timeout="20")
    拖拽元素 = xbot_visual.web.element.get_element(browser=web_page, select_type="xpath_selector", selector=None, css_selector="", xpath_selector="//div[@class=\"widgets__smooth_circle svcode-btn-m\"]", is_related_parent=False, parent=None, timeout="20")
    网页对象 = web_page
    缺口or单缺口 = "缺口识别"
    图鉴帐号 = ""
    图鉴密码 = ""
    重试次数 = 5
    偏移距离 = 0
    识别引擎 = "YD"
    移动速度 = "fast"
    开始拖动偏移量 = -155

    try:
        try:
            xbot_visual.web.browser.wait_load_completed(browser=网页对象, load_timeout="20", action_after_load_timeout="stopLoad")
        except Exception as e:
            xbot_visual.programing.log(type='info', text=xbot_visual.trace(e))
        
        assert str(重试次数).isdigit(), "【重试次数】类型为非负整数,请填写正确的参数类型!"
        assert int(重试次数)>=0, "【重试次数】类型为非负整数,请填写正确的参数类型!"
        
        for loop_index in xbot_visual.workflow.range_iterator(start="0", stop=重试次数, step="1"):
            if xbot_visual.web.browser.element_display(browser=网页对象, content_type="display", selector=图块所在背景):
                
                py2_args = {
                    "start_element": 目标图块,
                    "background_element": 图块所在背景,
                    "tj_username": 图鉴帐号,
                    "tj_password": 图鉴密码,
                    "web_page": 网页对象,
                    "缺口类型": 缺口or单缺口,
                    "ai_engine": 识别引擎,
                }
                py2(py2_args)
                滑动距离 = py2_args.get("distince", 0)

                # 拖动滑块
                bound = xbot_visual.web.element.get_bounding(browser=网页对象, element=拖拽元素, to96dpi=True, relative_to="screen", timeout="20")
                invoke_result = xbot_visual.process.invoke_module(module="utils", package=__name__, function="drag", params={
                    "point_x": lambda: int(bound.center_x) + 开始拖动偏移量,
                    "point_y": bound.center_y,
                    "distance": lambda: int(滑动距离) + 偏移距离,
                    "move_speed": 移动速度,
                })
                
            else:
                break
            
            xbot_visual.programing.sleep(random_number=True, seconds="1", start_number="2", stop_number="3")
    finally:
        pass

def py2(args):
    distince = 0
    if args is None:
        start_element = None
        background_element = None
        tj_username = ""
        tj_password = ""
        web_page = None
        缺口类型 = ""
        ai_engine = ""
    else:
        start_element = args.get("start_element", None)
        background_element = args.get("background_element", None)
        tj_username = args.get("tj_username", "")
        tj_password = args.get("tj_password", "")
        web_page = args.get("web_page", None)
        缺口类型 = args.get("缺口类型", "")
        ai_engine = args.get("ai_engine", "")
    
    try:
        background_bound2 = xbot_visual.web.element.get_bounding(browser=web_page, element=background_element, to96dpi=False, relative_to="screen", timeout="20")
        captcha_result = xbot_visual.programing.variable(value="")
        distince = xbot_visual.programing.variable(value=lambda: 0)
        ppi = xbot_visual.process.invoke_module(module="utils", package=__name__, function="get_ppi", params={})

        # 利用图鉴进行识别
        if xbot_visual.workflow.test(operand1=ai_engine, operator="==", operand2="TJ", operator_options="{}"):
            if xbot_visual.workflow.multiconditional_judgment(relation="and", conditionals=[{"operand1": 缺口类型,"operand2": "单缺口识别","operator": "=="}]):
                image_left = xbot_visual.web_service.captcha(engine_type="ttshitu", username=tj_username, password=xbot_visual.decrypt(tj_password), captcha_type="33", third_party_code="ttshitu_33_predict", image_source="web_element", image_file=None, image_browser=web_page, image_web_selector=background_element, image_window="", image_win_selector=None, image_region_x1="0", image_region_y1="0", image_region_x2="0", image_region_y2="0", imageback_source="screen", back_image_file=None, imageback_browser="", imageback_web_selector=None, imageback_window="", imageback_win_selector=None, imageback_region_x1="0", imageback_region_y1="0", imageback_region_x2="0", imageback_region_y2="0", typename=None, is_to96dpi=False, remark=None)
                distince = xbot_visual.programing.variable(value=lambda: int(image_left)*ppi)
            else:
                start_bound = xbot_visual.web.element.get_bounding(browser=web_page, element=start_element, to96dpi=False, relative_to="screen", timeout="20")
                captcha_result = xbot_visual.web_service.captcha(engine_type="ttshitu", username=tj_username, password=xbot_visual.decrypt(tj_password), captcha_type="18", third_party_code="ttshitu_18_predict", image_source="web_element", image_file=None, image_browser=web_page, image_web_selector=start_element, image_window="", image_win_selector=None, image_region_x1="0", image_region_y1="0", image_region_x2="0", image_region_y2="0", imageback_source="screen", back_image_file=None, imageback_browser="", imageback_web_selector=None, imageback_window="0", imageback_win_selector=None, imageback_region_x1=lambda: start_bound.right, imageback_region_y1=lambda: background_bound2.top, imageback_region_x2=lambda: background_bound2.right, imageback_region_y2=lambda: background_bound2.bottom, typename=None, is_to96dpi=False, remark=None)
                image_left = xbot_visual.text.extract_content_from_text(text=captcha_result, extract_way="custom", regular_pattern="(\\d+),", just_get_first=True, ignore_case=False)
                distince = xbot_visual.programing.variable(value=lambda: (int(image_left) + start_bound.width)*ppi)

        # 利用影刀引擎识别
        if xbot_visual.workflow.test(operand1=ai_engine, operator="==", operand2="YD", operator_options="{}"):
            if xbot_visual.workflow.multiconditional_judgment(relation="and", conditionals=[{"operand1": 缺口类型,"operand2": "单缺口识别","operator": "=="}]):
                image_left = xbot_visual.web_service.captcha(engine_type="shadowbot", username=None, password=xbot_visual.decrypt(""), captcha_type="33", third_party_code="ttshitu_33_predict", image_source="web_element", image_file=None, image_browser=web_page, image_web_selector=background_element, image_window="", image_win_selector=None, image_region_x1="0", image_region_y1="0", image_region_x2="0", image_region_y2="0", imageback_source="screen", back_image_file=None, imageback_browser="", imageback_web_selector=None, imageback_window="", imageback_win_selector=None, imageback_region_x1="0", imageback_region_y1="0", imageback_region_x2="0", imageback_region_y2="0", typename=None, is_to96dpi=False, remark=None)
                distince = xbot_visual.programing.variable(value=lambda: int(image_left)*ppi)
            else:
                start_bound = xbot_visual.web.element.get_bounding(browser=web_page, element=start_element, to96dpi=False, relative_to="screen", timeout="20")
                captcha_result = xbot_visual.web_service.captcha(engine_type="shadowbot", username=None, password=xbot_visual.decrypt(""), captcha_type="18", third_party_code="ttshitu_18_predict", image_source="web_element", image_file=None, image_browser=web_page, image_web_selector=start_element, image_window="", image_win_selector=None, image_region_x1="0", image_region_y1="0", image_region_x2="0", image_region_y2="0", imageback_source="screen", back_image_file=None, imageback_browser="", imageback_web_selector=None, imageback_window="0", imageback_win_selector=None, imageback_region_x1=lambda: start_bound.right, imageback_region_y1=lambda: background_bound2.top, imageback_region_x2=lambda: background_bound2.right, imageback_region_y2=lambda: background_bound2.bottom, typename=None, is_to96dpi=False, remark=None)
                image_left = xbot_visual.text.extract_content_from_text(text=captcha_result, extract_way="custom", regular_pattern="(\\d+),", just_get_first=True, ignore_case=False)
                distince = xbot_visual.programing.variable(value=lambda: (int(image_left) + start_bound.width)*ppi)

        # 利用本地引擎识别
        if xbot_visual.workflow.test(operand1=ai_engine, operator="==", operand2="LOCAL", operator_options="{}"):
            dir_path = xbot_visual.dir.get_special_dir(special_dir_name="TEMP")
            start_element_save_file_name = xbot_visual.web.element.screenshot(browser=web_page, capture_area="Element", element=start_element, folder_path=dir_path, random_filename=True, filename=None, overwrite_file=True, save_to_clipboard=False, height="25000", piece_height="3000", timeout="20")
            start_bound = xbot_visual.web.element.get_bounding(browser=web_page, element=start_element, to96dpi=False, relative_to="screen", timeout="20")
            background_bound = xbot_visual.web.element.get_bounding(browser=web_page, element=background_element, to96dpi=False, relative_to="screen", timeout="20")
            current_datetime = xbot_visual.datetime.now()
            text = xbot_visual.datetime.to_string(datetime=current_datetime, format="%Y%m%d%H%M%S")
            background_element_save_file_name = xbot_visual.programing.variable(value=xbot_visual.sh_str(dir_path) + "\\" + xbot_visual.sh_str(text) + ".png")
            _ = xbot_visual.system.take_screenshot(image_source="foreground_window", window=None, image_region="sub_region", region_x1=start_bound.right, region_y1=background_bound.top, region_x2=background_bound.right, region_y2=background_bound.bottom, save_to="file", image_path=background_element_save_file_name)
            invoke_result = xbot_visual.process.invoke_module(module="utils", package=__name__, function="distance_ty", params={
                "background_file": background_element_save_file_name,
                "gap_file": start_element_save_file_name,
            })
            distince = xbot_visual.programing.variable(value=lambda: int(start_bound.width/2+invoke_result*ppi))
            return distince
    finally:
        args["distince"] = distince

收藏6
全部评论1
最新
发布评论
评论