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

使用2Captcha API注入reCaptcha Token失效问题求助——针对西班牙司法部犯罪记录验证站点

Troubleshooting reCaptcha Token Injection on Spain's Justice Verification Site

Let me walk you through why your current approach isn't working and how to fix it. You're close—your code works on demo sites because they use a basic reCaptcha implementation, but the target site has additional checks to prevent direct token injection.

Key Issues with Your Current Code

Directly unhiding the g-recaptcha-response textarea and pasting the token skips reCaptcha's internal validation flow. The site likely checks if the token was generated through the official reCaptcha widget events, not manual DOM manipulation.

Step-by-Step Fixes

1. Use reCaptcha's Official setResponse Method

Instead of editing the textarea directly, use the reCaptcha API's built-in function to set the token. This triggers the necessary internal validation the site expects:

solution = solve()
code = solution['code']

# Wait for the reCaptcha script to fully initialize
WebDriverWait(driver, 10).until(
    lambda d: d.execute_script("return typeof grecaptcha !== 'undefined';")
)

# Set the token using reCaptcha's official method
driver.execute_script(f"grecaptcha.setResponse('{code}');")

If the page has multiple reCaptcha widgets, specify the widget ID:

widget_id = driver.execute_script("return grecaptcha.getWidgetIds()[0];")
driver.execute_script(f"grecaptcha.setResponse({widget_id}, '{code}');")

2. Add Basic User Interaction (Anti-Bot Mitigation)

Many sites check for human-like behavior before accepting reCaptcha tokens. Add simple mouse movements or clicks to the reCaptcha area:

from selenium.webdriver.common.action_chains import ActionChains

# Target the reCaptcha container (usually has class 'g-recaptcha')
recaptcha_container = driver.find_element(By.CLASS_NAME, "g-recaptcha")

# Simulate moving to the widget and clicking (mimics human behavior)
ActionChains(driver).move_to_element(recaptcha_container).click().perform()
time.sleep(2)

3. Hide Selenium Automation Traces

The site might detect you're using an automated browser. Add these Chrome options to bypass detection:

from selenium import webdriver

options = webdriver.ChromeOptions()
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)
# Override the webdriver property to hide automation
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
driver.get(page_url)

4. Verify Site Key and Token Validity

Double-check that:

  • Your site key 6Lf28LAUAAAAACso2fkzeLfJLKN1DBHUrVNpIN1b matches the one in the target site's source code (search for sitekey in page HTML).
  • The token returned by 2Captcha is a long string (hundreds of characters)—print code to confirm it's not empty or truncated.

Full Modified Code Snippet

Replace your injection/submit section with this:

solution = solve()
code = solution['code']

# Wait for reCaptcha to initialize
WebDriverWait(driver, 10).until(
    lambda d: d.execute_script("return typeof grecaptcha !== 'undefined';")
)

# Set token via official API
driver.execute_script(f"grecaptcha.setResponse('{code}');")

# Simulate human interaction
from selenium.webdriver.common.action_chains import ActionChains
recaptcha_container = driver.find_element(By.CLASS_NAME, "g-recaptcha")
ActionChains(driver).move_to_element(recaptcha_container).perform()
time.sleep(2)

# Submit the form
driver.find_element(By.ID, "submitConsulta").click()
time.sleep(10)

These changes should make the site recognize your reCaptcha token as valid, since you're using the official reCaptcha flow instead of manual DOM edits.

内容的提问来源于stack exchange,提问作者Julieta Benitez

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 19:52:42