Skip to content

Latest commit

 

History

12 Commits

Folders and files

Repository files navigation

reCAPTCHA v2 Solver for Python Selenium (CapMonster Cloud)

tests License: MIT Python 3.9+ Selenium 4

A runnable Python reCAPTCHA v2 solver for Selenium: it solves the Google reCAPTCHA v2 checkbox in a Selenium WebDriver session with the official CapMonster Cloud reCAPTCHA v2 solver Python SDK (capmonstercloudclient). Use it when Selenium tests or automation scripts stop at reCAPTCHA on forms you own or are allowed to automate.

What this example does

  1. Starts Chrome through Selenium 4 and opens a page with a reCAPTCHA v2 widget.
  2. Finds the sitekey in data-sitekey or in the widget iframe URL.
  3. Sends a RecaptchaV2Request to CapMonster Cloud with the browser's User-Agent and waits for the token.
  4. Writes the token into g-recaptcha-response, calls the page callback and can click your submit button.

Without arguments it uses the public demo page lessons.zennolab.com.

Requirements

  • Python 3.9+
  • Google Chrome (Selenium Manager downloads a matching chromedriver)
  • A CapMonster Cloud API key (see Get an API key)

Quick start

git clone https://github.com/CapMonsterCloud/python-selenium-recaptcha-v2-solver.git
cd python-selenium-recaptcha-v2-solver
pip install -r requirements.txt

export CAPMONSTER_API_KEY=your_key        # Windows: set CAPMONSTER_API_KEY=your_key
python examples/solve_recaptcha_v2.py --headed
python examples/solve_recaptcha_v2.py --url https://your-site.example/form --submit "button[type=submit]"

Tests (the browser test is skipped if Chrome is missing):

pytest -q

How it works

  • Sync Selenium, async SDK. Selenium's API is blocking and the SDK's solve_captcha is a coroutine. solve() wraps it in asyncio.run, so the example stays a plain script without an event loop around WebDriver calls.
  • Explicit wait for the iframe, not for the div. The .g-recaptcha container exists before Google's script runs. Waiting for iframe[src*='/recaptcha/'] guarantees the widget is actually rendered and its URL carries the sitekey.
  • Token through execute_script arguments. The token is passed as arguments[0] instead of being formatted into JavaScript, so long tokens with special characters never corrupt the script. The same call fires data-callback, which many forms use to enable the submit button.
  • One browser, one identity. The User-Agent sent to CapMonster Cloud is read from the running Chrome, which keeps the solved token consistent with the session that submits it.

reCAPTCHA v2 token basics

  • The token is a g-recaptcha-response string. Write it into the hidden textarea or pass it to the widget callback.
  • reCAPTCHA v2 tokens are single-use and short-lived. Request a new token for every submission and submit right away.
  • Pass the same page URL and sitekey that the browser used, otherwise the token can be rejected on validation.

Common errors

Symptom Cause and fix
TimeoutException waiting for the reCAPTCHA iframe The widget is lazy-loaded (scroll, click, modal). Trigger it before the wait or raise the timeout.
ElementClickInterceptedException on submit A cookie banner or overlay covers the button. Close it first or click via driver.execute_script("arguments[0].click()", el).
GetTaskError: [ERROR_KEY_DOES_NOT_EXIST] / [ERROR_ZERO_BALANCE] Wrong API key or empty balance.
RuntimeError: asyncio.run() cannot be called from a running event loop You run the code inside Jupyter or an async app. Use await client.solve_captcha(...) there instead of asyncio.run.
Server says the captcha is invalid The token expired (it is short-lived, roughly one to two minutes) or was submitted twice. Submit right after injection.
Two textareas filled, form still fails The page has several widgets with different sitekeys. Solve the one inside the form you submit.

Get an API key

This example needs a CapMonster Cloud account and API key. Register and get the key on the reCAPTCHA v2 solver page. There you will also find pricing and the API description. New users can ask support for a $0.10 test balance after registering.

Responsible use

Use this code only on websites you own or are explicitly authorized to test and automate. Follow each website's terms of service and applicable law, and keep your API key private.

Links


reCAPTCHA is a trademark of Google LLC. Selenium is a project of the Selenium community. CapMonster Cloud is not affiliated with or endorsed by these organizations.