Selenium Python中使用Anticaptcha插件无法解决ReCaptcha问题求助
ReCaptcha验证失败排查方案
常见问题及修复方法
- 不要直接用
innerHTML赋值验证码响应字段
直接修改innerHTML不会触发ReCaptcha绑定的DOM事件,ReCaptcha后台无法感知到响应值已填入,正确做法是先把隐藏的元素设为可见,再用value赋值,同时触发输入事件:
# 替换原来的execute_script赋值逻辑 captcha = captcha_solver() driver.execute_script(""" const responseEle = document.getElementById('g-recaptcha-response'); responseEle.style.display = 'block'; responseEle.value = arguments[0]; // 触发输入事件通知ReCaptcha值已更新 responseEle.dispatchEvent(new Event('input', { bubbles: true })); responseEle.dispatchEvent(new Event('change', { bubbles: true })); """, captcha)
- 修复Selenium自动化指纹特征
ReCaptcha会检测浏览器的自动化标识,需要在启动Driver前添加指纹隐藏配置:
from selenium import webdriver options = webdriver.ChromeOptions() # 关闭webdriver标识 options.add_argument('--disable-blink-features=AutomationControlled') options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(options=options) # 覆盖navigator.webdriver属性 driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', { 'source': ''' Object.defineProperty(navigator, 'webdriver', { get: () => undefined }) ''' })
- 校验参数匹配性
确认你提取的sitekey和提交页面的sitekey完全一致,调用anti-captcha时传入的url和你当前Selenium访问的页面url完全一致,不要出现http/https混用、域名不一致、路径不匹配的问题。 - 手动触发ReCaptcha回调
检查页面reCaptcha组件的data-callback属性,如果存在指定的回调函数,填入响应值后需要手动调用该回调:
# 假设data-callback值为onCaptchaSuccess driver.execute_script('onCaptchaSuccess(arguments[0])', captcha)
- 调整提交等待时长
填入验证码后不要立刻点击提交,建议延长等待时间到2-3秒,确保ReCaptcha内部校验逻辑执行完成。 - 确认验证码token时效性
anti-captcha返回的ReCaptcha响应token有效期仅为120秒,拿到响应值后需要立刻完成提交,超时token会自动失效。
内容的提问来源于stack exchange,提问作者Crazy Programming
相关产品推荐
相关产品推荐

