Solve CAPTCHAs in Crawl4AI crawls with DeathByCaptcha
A Crawl4AI crawl that stops at a protected form is not a failed crawl, it is a crawl that never reached the data. The fix is a navigation hook that asks DeathByCaptcha for the answer and hands it to the page before the extraction strategy reads anything.
Where the solve happens
Crawl4AI runs hooks around the navigation, and its own documentation
puts verification waits in after_goto: the page is loaded and the
widget, if there is one, is in the DOM. That is the whole design of
this example —
- the
on_page_context_createdhook blocks images, styles and fonts through route filtering: the run does not fetch what the solve does not need, and the challenge script still does; - the
after_gotohook detects the widget, reads itsdata-sitekey, asks for a token, writes it into the page, submits and waits for the success state; - the extraction strategy turns the result into structured JSON.
The solve runs in the worker, never in the browser: the credentials never reach the page and the token is returned as a value, so it stays out of the log. Nothing is published on PyPI. The example is a folder you copy.
Files
| File | What it is |
|---|---|
examples/crawl4ai/solver.py |
the solvers: plain functions over the official client, testable without a browser |
examples/crawl4ai/crawler.py |
the Crawl4AI cookbook: hooks, the token walk and a CSS extraction strategy |
examples/crawl4ai/README.md |
setup, hooks and limits |
The hooks
| Hook | What it does |
|---|---|
on_page_context_created |
route blocking — no images, styles or fonts |
after_goto |
the CAPTCHA walk: detect #recaptcha-demo, read data-sitekey, solve, inject, submit, wait |
| extraction strategy | JsonCssExtractionStrategy reads the success page as JSON |
The post-navigation hook in full:
async def solve_challenge(page, context, url, response, **kwargs):
"""The post-navigation hook: detect, solve, hand the token over,
submit and wait for the success state."""
widget = await page.query_selector("#recaptcha-demo")
if widget is None:
# No challenge on this page: nothing to solve, keep crawling.
return page
sitekey = await widget.get_attribute("data-sitekey")
token = SOLVED.get(url)
if token is None:
print(f"Solving the challenge for {url}")
# The client blocks while it polls: keep it off the event
# loop so the crawler keeps serving the browser.
token = await asyncio.to_thread(dbc.solve_recaptcha_v2, sitekey, url)
SOLVED[url] = token
await page.evaluate(INJECT_TOKEN, token)
await page.click("#recaptcha-demo-submit")
# The submit navigates: wait for the success page before the
# crawler takes its snapshot, or the result is the form again.
await page.wait_for_selector(".recaptcha-success", timeout=30000)
LAST_HTML[url] = await page.content()
print("Token injected, form submitted")
return page
The official Python client is synchronous: a solve inside the hook
would freeze the crawler's event loop for up to two minutes while the
browser waits. asyncio.to_thread keeps both running.
Setup
pip install deathbycaptcha-official
pip install -r examples/crawl4ai/requirements.txt
crawl4ai-setup # installs the browser, once
export DBC_USERNAME=your_username
export DBC_PASSWORD=your_password
# or: export DBC_AUTHTOKEN=your_authtoken
The example needs Python 3.10 or newer (Crawl4AI's own requirement).
Run
python examples/crawl4ai/crawler.py
The tiny page that looks like a block
This is the part that cost the most and readers will hit it too. The walk works — solve, inject, submit, the success page arrives, the extraction returns
[{"success": "Verification Success... Hooray!"}]
— and the crawl still comes back success=False:
Error: Blocked by anti-bot protection: Structural:
minimal_text, no_content_elements (426 bytes, 31 chars visible)
The success page of Google's demo is a single line. Crawl4AI's
anti-bot check reads a page that small as a block page — and its own
documentation calls this a cheap false positive whose rescue is the
fallback_fetch_function. The example returns the page the crawl
actually produced, keyed by URL:
async def rescue_fetch(url):
return LAST_HTML.get(url)
A URL the crawl never produced returns nothing, so a real block still fails the way it should.
The two balance guards
- One token per URL. The solved map is keyed by page: a retried run re-injects the token it already bought instead of paying for the same solve twice.
- The example never retries silently.
arunreturns a failed result and the script raises: a failed solve must not burn the balance on attempts nobody sees.
The LLM half of the pipeline
Crawl4AI is built for AI extraction: this example ships the CSS
strategy because it needs no API key and runs in the tests. The LLM
strategies slot into the same CrawlerRunConfig(extraction_strategy=...)
position — see the
LLM strategies
of the Crawl4AI documentation when the page needs reasoning instead of
selectors. The CAPTCHA walk does not change: the hook still hands the
page over solved, and the strategy still reads it afterwards.
What the crawl does
- Visits Google's reCAPTCHA demo — the vendor's own page, not a third party's production site.
- The
on_page_context_createdhook blocks the static assets. - The
after_gotohook reads thedata-sitekeyfrom the page instead of typing it in and asks for a token. - It writes the token into
#g-recaptcha-responseand calls the widget's callback — the same walk as the Selenium, Playwright, Crawlee, Puppeteer, Cypress and WebdriverIO samples across the two clients. - Submits, waits for
.recaptcha-successand lets the CSS strategy extract the result as JSON.
The rule that catches people out
A token answer is valid only from the exit IP that solved it. The solve
runs on the machine that runs the crawl, so a solve and the request it
is pasted into leave from the same machine — unless your crawl leaves
through a proxy, in which case pass the same proxy and proxytype in
the parameters.
Limits
- QA on your own pages. These crawls are for flows you are allowed
to automate — staging, a demo, a page you own. See
RESPONSIBLE_USE.md. - reCAPTCHA v3 needs the
actionand themin_scorethe site expects; a mismatch is a token the site refuses. - The sandbox: Crawl4AI launches Chromium with it on, which a CI
container rarely supports. The example turns it off with
extra_args=["--no-sandbox"]; delete the line where your runner has a working sandbox. - Never print the token: the solvers return it as a value and nothing else should echo it.
Tests
python -m pytest tests/test_crawl4ai_solver.py -v # no network
DBC_TEST_USERNAME=... DBC_TEST_PASSWORD=... python -m pytest tests/test_crawl4ai_integration.py -v
The unit tests replace the client with a mock. The integration test runs the real crawler against the live API, checks that it extracted the verification and fails if anything it prints looks like a token. It skips itself when Crawl4AI or the credentials are not in the environment, and it runs in the continuous integration of the repository on every push.
Next steps
- Example: https://github.com/deathbycaptcha/deathbycaptcha-api-client-python
- API documentation: https://deathbycaptcha.com/api

English
Spanish
Russian
Chinese
French
Hindi
Arabic
Bengali
Indonesian
Portuguese
com, 