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.
- Starts Chrome through Selenium 4 and opens a page with a reCAPTCHA v2 widget.
- Finds the sitekey in
data-sitekeyor in the widget iframe URL. - Sends a
RecaptchaV2Requestto CapMonster Cloud with the browser's User-Agent and waits for the token. - 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.
- Python 3.9+
- Google Chrome (Selenium Manager downloads a matching chromedriver)
- A CapMonster Cloud API key (see Get an API key)
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- Sync Selenium, async SDK. Selenium's API is blocking and the SDK's
solve_captchais a coroutine.solve()wraps it inasyncio.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-recaptchacontainer exists before Google's script runs. Waiting foriframe[src*='/recaptcha/']guarantees the widget is actually rendered and its URL carries the sitekey. - Token through
execute_scriptarguments. The token is passed asarguments[0]instead of being formatted into JavaScript, so long tokens with special characters never corrupt the script. The same call firesdata-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.
- The token is a
g-recaptcha-responsestring. 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.
| 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. |
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.
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.
- Python SDK: CapMonster Cloud Python SDK
- reCAPTCHA v2 solving with CapMonster Cloud: capmonster.cloud/en/recaptcha-v2
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.