您现在的位置是:网站首页 > 博客日记 >

playwright|selenium模拟人工操作轨迹拖动滑块|动作链

作者:YXN-python 阅读量:50 发布日期:2024-12-10

playwright版

使用教程參考:playwright异步通过滑动验证码实例|支持多线程

async def human_like_slide(page, slider_btn, move_distance):
    """
    模拟人工操作轨迹拖动滑块

    :param page: 可操作的页面对象
    :param slider_btn: 可操作的滑块对象
    :param move_distance: 移动距离,单位为像素
    """
    move_distance = move_distance
    # 获取滑块的中心点位置
    bounding_box = await slider_btn.bounding_box()

    start_x = bounding_box['x'] + bounding_box['width'] / 2
    start_y = bounding_box['y'] + bounding_box['height'] / 2

    # 生成滑动轨迹:轨迹总和严格等于 move_distance
    def get_track(distance):
        tracks = []
        current = 0
        mid = distance * random.uniform(0.6, 0.8)  # 前60%-80%快速移动
        while current < distance:
            if current < mid:
                step_ = random.randint(10, 20)  # 快速移动步长较大
            else:
                step_ = random.randint(2, 8)  # 缓慢移动步长较小
            if current + step_ > distance:
                step_ = distance - current  # 调整最后一步
            tracks.append(step_)
            current += step_
        return tracks

    track = get_track(move_distance)

    # 按下滑块
    await slider_btn.hover()
    await page.mouse.move(start_x, start_y)
    await page.mouse.down()

    # 模拟拖动过程
    for step in track:
        start_x += step
        # 模拟小幅度上下偏移
        offset_y = random.randint(-1, 1)
        await page.mouse.move(start_x, start_y + offset_y, steps=5)  # 减少steps提升速度
        await asyncio.sleep(random.uniform(0.01, 0.03))  # 更短暂停时间

    # 松开滑块
    await page.mouse.up()

selenium版

def human_like_slide(driver, slider_btn, move_distance):
    """
    模拟人工操作轨迹拖动滑块

    :param driver: 驱动对象
    :param slider_btn: 滑块对象
    :param move_distance: 移动距离,单位为像素
    """
    def get_track(distance):
        """
        生成一个模拟人类滑动的轨迹
        """
        tracks = []
        current = 0
        mid = distance * random.uniform(0.6, 0.8)  # 前60%-80%快速移动
        while current < distance:
            if current < mid:
                step_ = random.randint(10, 20)  # 快速移动步长较大
            else:
                step_ = random.randint(2, 8)  # 缓慢移动步长较小
            if current + step_ > distance:
                step_ = distance - current  # 调整最后一步
            tracks.append(step_)
            current += step_
        return tracks

    track = get_track(move_distance)

    # 使用ActionChains类模拟拖动行为
    action = ActionChains(driver)
    action.click_and_hold(slider_btn).perform()  # 按下滑块

    # 模拟拖动过程
    for step in track:
        # 模拟小幅度上下偏移
        offset_y = random.randint(-5, 5)
        action.move_by_offset(step, offset_y).perform()

    # 松开滑块
    action.release().perform()

简单的

方式1: 一下拖到顶
actions = ActionChains(bro)  # 拿到动作链对象
actions.drag_and_drop(sourse, target)  # 把动作放到动作链中,准备串行执行
actions.perform()

方式2: 慢慢拖
ActionChains(bro).click_and_hold(sourse).perform()
distance = target.location['x'] - sourse.location['x']
track = 0
while track < distance:
    ActionChains(bro).move_by_offset(xoffset=2, yoffset=0).perform()
    track += 2

YXN-python

2024-12-10