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

如何使用Selenium定位元素及在指定网站获取reCAPTCHA的data-sitekey以绕过验证

Hey there! Let's tackle your two Selenium questions in detail:

1. How to Locate Elements with Selenium

Selenium provides several flexible ways to target elements, depending on the element's attributes and the page's structure. Here are the most common methods with practical examples:

  • ID Locator: The most reliable option when elements have unique IDs.
    driver.find_element(By.ID, "unique-element-id")
    
  • Class Name Locator: Works for elements with a single class (skip compound classes with spaces—use CSS selectors instead for those cases).
    driver.find_element(By.CLASS_NAME, "single-class-name")
    
  • CSS Selector: Powerful and versatile, great for targeting elements with specific attributes or nested structures.
    # Target a div with class 'g-recaptcha' that has a data-sitekey attribute
    driver.find_element(By.CSS_SELECTOR, ".g-recaptcha[data-sitekey]")
    
  • XPath: Useful for complex hierarchical layouts or when other locators aren't feasible.
    driver.find_element(By.XPATH, "//div[@class='g-recaptcha']")
    
  • Name Locator: Ideal for form elements with a unique name attribute.
    driver.find_element(By.NAME, "username")
    
  • Link Text/Partial Link Text: For targeting anchor tags by their visible display text.
    driver.find_element(By.LINK_TEXT, "Buy Tickets Now")
    
2. How to Get reCAPTCHA's data-sitekey on https://premier.hkticketing.com/

Let's first fix the issues with your current code, then walk through the correct approach:

What's Wrong with Your Existing Code

  • Your first snippet uses EC.element_to_be_clickable, but the g-recaptcha div itself isn't clickable (the interactive checkbox lives inside its nested iframe). Waiting for clickability here will trigger a timeout.
  • Your second snippet tries to switch to an iframe with class g-recaptcha, but g-recaptcha is the class of the outer div wrapping the reCAPTCHA iframe—not the iframe itself. That's why you get the "Unable to locate element" error.

Correct Approach to Fetch data-sitekey

The data-sitekey attribute is stored directly on the outer g-recaptcha div, not inside the iframe. You just need to wait for this div to load in the DOM (not for it to be clickable):

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

# Wait up to 10 seconds for the g-recaptcha div to be present in the DOM
g_recaptcha_div = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CLASS_NAME, "g-recaptcha"))
)

# Extract the data-sitekey attribute
data_sitekey = g_recaptcha_div.get_attribute("data-sitekey")
print(data_sitekey)

Edge Cases to Consider

  • If the site uses an invisible reCAPTCHA, the data-sitekey might be attached to a button or hidden element. In that case, use a CSS selector to target any element with the attribute:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "[data-sitekey]"))
    )
    data_sitekey = element.get_attribute("data-sitekey")
    
  • Ensure you're not inside an iframe when trying to locate the g-recaptcha div. If you previously switched to an iframe, switch back to the default content first with driver.switch_to.default_content().

Important Note

Bypassing reCAPTCHA often violates a website's Terms of Service. reCAPTCHA is designed to prevent automated access, and modern versions (v2/v3) have strong anti-bot mechanisms that make reliable bypass extremely difficult. Always use automation in compliance with the site's rules.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 17:08:37