使用Python Selenium自动化登录巴西MEI DAS付款网站时如何绕过hCaptcha验证
嘿,我完全懂你现在的困扰——本来想靠Selenium自动搞定MEI DAS的付款下载,省掉手动操作的麻烦,结果被hCaptcha的13896错误拦下来,试过了禁用检测和拟人化操作还是没用,确实让人头大。先帮你梳理下你已经尝试的方法:
遇到的错误提示:
"13896 - Prevented by hCaptcha protection. Robot behavior."
已尝试的方案:
✅ 禁用Selenium自动化检测:options = webdriver.ChromeOptions() options.add_argument("--disable-blink-features=AutomationControlled") navegador = webdriver.Chrome(options=options)✅ 基础拟人化交互:
- 操作间添加随机延迟
- 用ActionChains点击前移动鼠标
- 交互前模拟页面滚动
接下来给你几个更进阶的思路,说不定能解决问题:
1. 强化反检测配置,彻底模糊Selenium特征
只禁用AutomationControlled还不够,Chrome还有很多其他指纹会暴露你在用自动化工具。试试把这些参数加上:
from selenium import webdriver options = webdriver.ChromeOptions() # 最大化窗口,模拟真实用户的浏览状态 options.add_argument("--start-maximized") # 禁用扩展和插件检测 options.add_argument("--disable-extensions") options.add_argument("--disable-plugins-discovery") # 避免沙箱模式带来的特征暴露 options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") # 设置真实的User-Agent,不要用Selenium默认的 options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36") # 关闭密码保存、通知弹窗这类干扰项,更像真实用户设置 options.add_experimental_option("prefs", { "credentials_enable_service": False, "profile.password_manager_enabled": False, "profile.default_content_setting_values.notifications": 2 }) # 隐藏自动化相关的开关和扩展 options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) navegador = webdriver.Chrome(options=options) # 最后再手动覆盖navigator.webdriver属性 navegador.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
2. 把拟人化操作做得更“像人”
你已经做了基础的拟人化,但可以再细化细节,让操作轨迹更随机:
- 延迟不要用固定值,改用随机范围,比如:
import time import random time.sleep(random.uniform(1.2, 3.8)) # 模拟人类不确定的等待时间 - 鼠标移动不要直接跳转到目标元素,先绕点路:
from selenium.webdriver.common.action_chains import ActionChains # 先随机移动到页面某个无关位置 random_x = random.randint(100, 800) random_y = random.randint(100, 600) ActionChains(navegador).move_by_offset(random_x, random_y).perform() time.sleep(random.uniform(0.4, 1.1)) # 再慢慢移到目标元素 target_element = navegador.find_element(By.ID, "your-target-id") ActionChains(navegador).move_to_element(target_element).perform() time.sleep(random.uniform(0.2, 0.7)) # 点击前模拟轻微晃动,更像人类操作 for _ in range(2): ActionChains(navegador).move_by_offset(random.randint(-3,3), random.randint(-3,3)).perform() time.sleep(0.1) ActionChains(navegador).click(target_element).perform() - 滚动页面时也用随机步长,不要直接滚到指定位置:
# 随机滚动几次 for _ in range(random.randint(2,4)): scroll_amount = random.randint(150, 400) navegador.execute_script(f"window.scrollBy(0, {scroll_amount})") time.sleep(random.uniform(0.5, 1.2))
3. 试试undetected-chromedriver这个神器
这是一个专门针对反爬修改的ChromeDriver版本,它会自动帮你处理大部分Selenium检测问题,比手动加参数省心多了。安装和使用方法:
pip install undetected-chromedriver
import undetected_chromedriver as uc # 初始化浏览器,它会自动隐藏所有自动化特征 navegador = uc.Chrome() navegador.get("你的MEI DAS网站地址")
很多时候用这个工具就能直接绕过hCaptcha的基础拦截。
4. 考虑第三方验证码解决方案(谨慎使用)
如果上面的方法都不行,hCaptcha的图像验证确实很难纯靠Selenium破解。这时可以考虑合规的人工打码服务或者反验证码API,这类服务能自动处理hCaptcha验证,你只需要调用API获取结果再传入浏览器。不过要注意,一定要确认网站的服务条款允许使用这类工具,避免账号被封禁。
最后提醒下:hCaptcha的检测规则一直在更新,没有永久有效的方法,你可能需要根据实际情况调整策略。另外,一定要遵守网站的使用规范,避免触发更严格的封禁机制。
备注:内容来源于stack exchange,提问作者Diego Freire

