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

使用Python+Selenium解析reCAPTCHA V2时遇元素定位失败问题

解决reCAPTCHA V2解析中Selenium找不到textarea元素的问题

问题核心

你遇到的NoSuchElementException主要源于三点:

  • 用标签名textarea定位太宽泛,页面可能存在多个同名标签,或目标元素未完成加载
  • reCAPTCHA的g-recaptcha-response文本框默认处于隐藏状态(display: none),直接定位易失败
  • 代码中display: true是错误的CSS属性值,正确显示属性应为block/inline等

修正方案及代码

以下是调整后的完整代码,关键改动已标注:

import sys
import time
import requests
import os
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from twocaptcha import TwoCaptcha

"""Captcha Resolver for v2"""
def captcharesolver(driver):
    solver = TwoCaptcha(MY_API_KEY)

    try:
        result = solver.recaptcha(
            sitekey=SITE_KEY,
            url=SITE_URL)
    except Exception as e:
        sys.exit(e)
    else:
        response_site = requests.get(SITE_URL)
        
        # Requesting RECAPTCHA ID 
        response = requests.get(REQUEST_URL, params= {
                                 "key": os.environ.get("API_KEY"), 
                                 "method": "userrecaptcha", 
                                 "googlekey": SITE_KEY, 
                                 "pageurl": SITE_URL
                                })
        
        # Getting the token
        token_from_captcha = response.text.split("|")[1]
        time.sleep(15)

        # Get the Captcha solver 
        while True:
            time.sleep(10)
            full_request_solution = requests.get(RESPONSE_URL, params={
                'key': MY_API_KEY, 
                'action': 'get', 
                'id':token_from_captcha
                })
            
            if full_request_solution.text[0:2] == 'OK':
                break
        final_req = full_request_solution.text
        final_request_solution = final_req.split("|")[1]
        
        # --- 关键改动区域 ---
        try:
            # 显式等待元素加载,超时10秒
            captcha_textarea = WebDriverWait(driver, 10).until(
                EC.presence_of_element_located((By.NAME, "g-recaptcha-response"))
            )
            # 修正CSS属性,让元素可见
            driver.execute_script("arguments[0].setAttribute('style', 'display: block; visibility: visible;')", captcha_textarea)
            # 直接设置textarea的value属性(比innerText更可靠)
            driver.execute_script("arguments[0].value = arguments[1]", captcha_textarea, final_request_solution)
        except TimeoutException:
            print("超时:未找到g-recaptcha-response文本框")
            sys.exit(1)

关键调整说明

  1. 精准定位:通过name="g-recaptcha-response"直接定位目标元素,避免标签名定位的歧义
  2. 显式等待:用WebDriverWait替代固定sleep,确保元素渲染完成后再操作,适配页面加载延迟
  3. 正确显示属性:将错误的display: true改为display: block; visibility: visible;,确保元素从隐藏转为可见
  4. 可靠赋值:通过value属性设置textarea内容,而非innerText,符合表单元素的内容存储规则

额外排查点

如果仍定位失败,检查页面是否存在嵌套iframe:部分reCAPTCHA v2会将验证框放在iframe内,需先执行driver.switch_to.frame(iframe_element)切换上下文,再定位元素。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 12:08:10