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

使用2Captcha API处理Recaptcha V2 Invisible时遇错误及验证失败问题

解决2Captcha API处理Recaptcha V2 Invisible时的错误与验证问题

在自动化浏览场景中使用2Captcha API解决Recaptcha V2 Invisible,已参考2Captcha官网、YouTube及StackOverflow相关教程,但运行代码时出现'dict' object is not callable错误,且获取API响应结果后无法完成验证码验证,请求协助解决代码错误及验证码提交问题。

原代码

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.proxy import *
from time import sleep
import time
import openpyxl
import requests
import pandas as pd

from solveRecaptcha import solveRecaptcha

# Load the Excel file
df = pd.read_excel('file.xlsx')

# Loop through the rows of the DataFrame
for index, row in df.iterrows():
    # Print the current row
    print('Current row: /n', row)
    
    # Do your processing here
    username = row['Name']
    password = row['Pass']

    # Create a new instance of the webdriver with the updated capabilities
    # Use selenium to log in with the username and password
    driver = webdriver.Chrome('./chromedriver')
    driver.get("https://www.reddit.com/account/register/")
    driver.find_element(By.XPATH, "/html/body/div/main/div[1]/div/div[2]/form/fieldset[2]/button").click()
    sleep(3)
    driver.find_element(By.XPATH, "/html/body/div/main/div[2]/div/div/div[2]/div[1]/form/fieldset[1]/input[2]").send_keys(username)
    driver.find_element(By.CSS_SELECTOR, "input#regPassword").send_keys(password)
    sleep(10)

    result = solveRecaptcha(
        "6LeTnxkTAAAAAN9QEuDZRpn90WwKk_R1TRW_g-JC",
        "https://www.reddit.com/account/register/",
    )
    print(result)

    sleep(10)

    code = result('code')

    WebDriverWait(driver, 10).until(
        EC.presence_of_all_elements_located((By.ID, 'g-recaptcha-response'))
    )

    driver.execute_script(
        "document.getElementByID('g-recaptcha-response').innerHTML = " + "'" + code + "'"
    )


    driver.find_element(By.ID, "recaptcha-verify-button").click()

    sleep(77)

问题分析与解决方案

1. 解决'dict' object is not callable错误

错误出在code = result('code')这一行:solveRecaptcha返回的是字典类型数据,字典取值需要用方括号[],而非圆括号()。修正为:

code = result['code']

2. 修复验证码提交失败的问题

以下几处问题会导致验证码无法正确提交:

  • JavaScript方法名错误:getElementByID应为getElementById(注意Id是小写d)
  • Input元素赋值错误:g-recaptcha-response是input元素,需修改它的value属性,而非innerHTML
  • 等待条件错误:用presence_of_element_located等待单个元素即可,无需使用presence_of_all_elements_located
  • 进程泄漏问题:每次循环创建新的driver实例后未关闭,会导致大量Chrome进程残留,需在循环末尾添加driver.quit()

修正后的核心代码片段

# 获取验证码token
result = solveRecaptcha(
    "6LeTnxkTAAAAAN9QEuDZRpn90WwKk_R1TRW_g-JC",
    "https://www.reddit.com/account/register/",
)
code = result['code']

# 等待g-recaptcha-response元素出现
WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, 'g-recaptcha-response'))
)

# 注入验证码token到页面
driver.execute_script(f"document.getElementById('g-recaptcha-response').value = '{code}';")

# 触发验证码验证(Invisible Recaptcha部分场景需调用此方法,视页面情况调整)
driver.execute_script("grecaptcha.execute();")

# 部分页面无需手动点击验证按钮,若需要则保留以下代码
# driver.find_element(By.ID, "recaptcha-verify-button").click()

# 处理完成后关闭driver,避免进程泄漏
sleep(5)
driver.quit()

额外提示

  • 尽量用WebDriverWait等待元素状态变化,替代硬编码的sleep(),提升代码稳定性
  • 执行前确认solveRecaptcha函数能正确返回包含code字段的字典,可通过print(result)检查返回格式

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 03:41:45