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

如何使用Python Selenium绕过滑块验证码?

绕过滑块验证码的实用方案(基于Python Selenium)

先优化Selenium的反检测能力

很多网站会先识别出你使用的是自动化工具,才触发滑块验证码。先把这部分特征隐藏:

  • 用undetected-chromedriver替代原生Selenium:这个库专门处理Chrome的自动化检测,能自动规避window.navigator.webdriver等特征检测,用法和原生Selenium几乎一致:
from undetected_chromedriver import Chrome, ChromeOptions

options = ChromeOptions()
# 可选:添加无头模式、禁用图片加载等参数
options.add_argument("--headless=new")
options.add_argument("--blink-settings=imagesEnabled=false")
driver = Chrome(options=options)
  • 模拟真实用户的操作间隔:不要用固定的time.sleep(2),改用随机等待时间,比如time.sleep(random.uniform(1.2, 3.5)),避免机械性的操作节奏。

模拟人类风格的滑块拖动

就算绕过了工具检测,滑块操作太生硬也会被识别,要模拟真实拖动轨迹:

  1. 计算滑块需要移动的距离:先定位滑块和缺口的元素,获取它们的X轴坐标差值(注意调整页面偏移或元素边框的误差):
slider = driver.find_element(By.CSS_SELECTOR, ".slider-btn")  # 替换成你的滑块选择器
gap = driver.find_element(By.CSS_SELECTOR, ".gap-box")  # 替换成你的缺口选择器
distance = gap.location["x"] - slider.location["x"] - 8  # 8是修正值,根据实际情况调整
  1. 模拟带加速度、微调的拖动轨迹:不要直接一次性拖到位,要模拟人类拖动时的加速、停顿、微调动作:
from selenium.webdriver.common.action_chains import ActionChains
import random
import time

actions = ActionChains(driver)
actions.click_and_hold(slider).perform()

# 先快速拖动大部分距离
actions.move_by_offset(distance * 0.75, random.randint(-3, 3)).perform()
time.sleep(random.uniform(0.15, 0.3))

# 中间停顿修正
actions.move_by_offset(distance * 0.2, random.randint(-1, 1)).perform()
time.sleep(random.uniform(0.1, 0.2))

# 最后微调到位
actions.move_by_offset(distance * 0.05, random.randint(-1, 1)).perform()
time.sleep(random.uniform(0.05, 0.1))

actions.release().perform()

备选方案

如果上述方法还是无法通过验证,可以考虑:

  • 打码平台:对接第三方人工打码服务,把滑块验证码的截图传给平台,获取拖动距离后再模拟操作(注意合规性和成本)。
  • 切换API调用:如果能找到目标网站的后端API接口,直接发送请求获取数据,比模拟浏览器更稳定,也不会触发验证码。

内容的提问来源于stack exchange,提问作者jacopo salmistrari

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 15:47:15