Skip to content
CaptchaSonic

Selenium 集成

Selenium + CaptchaSonic

Python 优先的验证码解决方案,适用于 Selenium WebDriver 自动化项目。

npm

快速安装

pip install captchasonic selenium

01

导入并配置

设置 Selenium WebDriver 和 CaptchaSonic Python SDK。

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

# Solves over a native gRPC connection (no HTTP overhead)
client = CaptchaSonic("sonic_your_key_here")

02

导航到目标

启动浏览器并导航到带有验证码的页面。

driver = webdriver.Chrome()
driver.get("https://example.com/login")

# Wait for captcha to load
WebDriverWait(driver, 10).until(
    EC.presence_of_element_located(
        (By.CSS_SELECTOR, "iframe[src*='recaptcha']")
    )
)

03

提取 sitekey 并解决

获取验证码参数并发送到 CaptchaSonic。

# Get the sitekey from the page
sitekey = driver.find_element(
    By.CSS_SELECTOR, "[data-sitekey]"
).get_attribute("data-sitekey")

# Solve with CaptchaSonic over gRPC
result = client.solve_recaptcha_v2_token(
    website_url=driver.current_url,
    website_key=sitekey,
)

token = result["solution"]["gRecaptchaResponse"]
print(f"Token: {token}")

04

注入并提交

应用解决方案令牌并提交表单。

# Inject the token
driver.execute_script(f"""
    document.getElementById('g-recaptcha-response')
        .innerHTML = '{token}';
""")

# Submit the form
driver.find_element(By.CSS_SELECTOR, "button[type='submit']").click()

# Wait for page transition
WebDriverWait(driver, 10).until(
    EC.url_changes(driver.current_url))
print("Captcha bypassed successfully!")

Links

相关资源

准备好自动化了吗?

获取您的 API 密钥,几分钟内开始解决验证码。包含免费 $0.50 奖励积分。

获取 API 密钥