You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python/Selenium实现类人鼠标移动?求Ghost Cursor类工具及集成方案

模拟类人鼠标操作的Selenium集成方案与替代模块

我尝试了Stack Overflow上的类人鼠标移动代码,但不清楚如何将其集成到实际爬虫中,针对不同DOM元素执行鼠标操作。想知道是否存在类似Node.js/pptr中Ghost Cursor的Python模块?或者有可行的集成方案?我已提交Selenium功能请求。

一、类似Ghost Cursor的Python替代模块

  • mouse-move-selenium:第三方库,专门为Selenium生成类人鼠标移动路径,内置贝塞尔曲线和随机偏移逻辑,直接兼容ActionChains
  • selenium-stealth:主打反爬虫检测,可配合自定义鼠标移动逻辑使用,增强爬虫的隐蔽性
  • pyautogui:支持生成自然的鼠标移动轨迹,但需要注意Selenium浏览器窗口与系统坐标的映射问题,适合桌面端爬虫场景

二、可行的Selenium集成方案

核心思路是:动态生成从当前鼠标位置到目标元素的自然路径,通过分步移动+随机延迟模拟人类操作。以下是可复用的集成代码:

完整集成代码

import numpy as np
import scipy.interpolate as si
from time import sleep, random
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

def generate_human_path(start_x, start_y, end_x, end_y, steps=100):
    # 生成随机中间控制点,模拟人类鼠标的自然偏移
    control_points = [
        [start_x + random()*(end_x-start_x)*0.4, start_y + random()*(end_y-start_y)*0.6],
        [start_x + random()*(end_x-start_x)*0.6, start_y + random()*(end_y-start_y)*0.4]
    ]
    points = np.array([[start_x, start_y]] + control_points + [[end_x, end_y]])
    x = points[:, 0]
    y = points[:, 1]
    
    t = range(len(points))
    ipl_t = np.linspace(0.0, len(points)-1, steps)
    
    x_tup = si.splrep(t, x, k=3)
    y_tup = si.splrep(t, y, k=3)
    
    x_list = list(x_tup)
    xl = x.tolist()
    x_list[1] = xl + [0.0, 0.0, 0.0, 0.0]
    
    y_list = list(y_tup)
    yl = y.tolist()
    y_list[1] = yl + [0.0, 0.0, 0.0, 0.0]
    
    return si.splev(ipl_t, x_list), si.splev(ipl_t, y_list)

def move_to_element_human(driver, target_element):
    # 获取目标元素的中心坐标
    elem_rect = target_element.rect
    target_x = elem_rect['x'] + elem_rect['width'] / 2
    target_y = elem_rect['y'] + elem_rect['height'] / 2
    
    # 获取当前鼠标位置(初始为浏览器左上角,可根据实际操作更新)
    current_x, current_y = 0, 0
    
    # 生成类人移动路径
    x_path, y_path = generate_human_path(current_x, current_y, target_x, target_y)
    
    # 初始化动作链,移动到起点
    action = ActionChains(driver)
    action.move_by_offset(current_x, current_y).perform()
    
    # 分步执行移动,加入随机延迟
    for idx, (x, y) in enumerate(zip(x_path, y_path)):
        # 计算相对上一步的偏移量
        offset_x = x - (x_path[idx-1] if idx > 0 else current_x)
        offset_y = y - (y_path[idx-1] if idx > 0 else current_y)
        
        action = ActionChains(driver)
        action.move_by_offset(offset_x, offset_y).perform()
        # 模拟人类操作的微小停顿
        sleep(random() * 0.06)

# 示例使用
if __name__ == "__main__":
    driver = webdriver.Chrome()
    driver.get("https://example.com")
    
    # 定位目标DOM元素(替换为你的选择器)
    target_element = driver.find_element(By.CSS_SELECTOR, "h1")
    
    # 执行类人鼠标移动
    move_to_element_human(driver, target_element)
    
    driver.quit()

关键优化说明

  1. 动态路径生成:不再依赖固定曲线点,而是根据起点和终点自动生成带随机控制点的贝塞尔曲线,还原人类鼠标的自然偏移
  2. 坐标适配:通过元素的rect属性获取绝对坐标,自动计算元素中心作为目标点,适配不同尺寸的DOM元素
  3. 随机延迟:在每步移动后加入0-0.06秒的随机停顿,避免机械性的匀速移动
  4. 偏移重置:每次移动都重新初始化ActionChains,解决累积偏移导致的定位错误

内容的提问来源于stack exchange,提问作者Mévatlavé Kraspek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 20:12:04