Solve CAPTCHAs in Crawlee crawlers with DeathByCaptcha
A Crawlee 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 handler reads anything.
Where the solve happens
Crawlee runs two hooks around every navigation, and its own documentation marks the post-navigation hook as the place for CAPTCHA checks: the page is loaded and the widget, if there is one, is in the DOM. That is the whole design of this example —
- the pre-navigation hook keeps the run fast:
block_requests()stops images, styles and fonts from loading, and the challenge script still does; - the post-navigation hook detects the widget, reads its
data-sitekey, asks for a token, writes it into the page and submits; - the request handler waits for the success state and pushes the outcome to the dataset.
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 as an npm package or on PyPI. The example is a folder you copy.
Files
| File | What it is |
|---|---|
examples/crawlee/deathbycaptcha.solve.js (Node) / solver.py (Python) |
the solvers: promises or plain functions over the official client, testable without a browser |
examples/crawlee/crawler.js / crawler.py |
the Crawlee cookbook: a PlaywrightCrawler with the hooks and the handler |
examples/crawlee/README.md |
setup, hooks and limits |
The hooks
| Hook | What it does |
|---|---|
preNavigationHooks / pre_navigation_hook |
block_requests() — no images, styles or fonts; the challenge script still loads |
postNavigationHooks / post_navigation_hook |
the CAPTCHA walk: detect #recaptcha-demo, read data-sitekey, solve, inject, submit |
requestHandler / request_handler |
waits for .recaptcha-success, pushes the outcome and prints CRAWL_OK |
The post-navigation hook in the Node crawler:
postNavigationHooks: [
async ({ page, request, log }) => {
const widget = await page.$('#recaptcha-demo');
if (!widget) {
// No challenge on this page: nothing to solve, keep crawling.
return;
}
const sitekey = await widget.getAttribute('data-sitekey');
const pageurl = request.url;
let token = solved.get(pageurl);
if (!token) {
log.info(`Solving the challenge for ${pageurl}`);
token = await solver.recaptchaV2({ sitekey, pageurl });
solved.set(pageurl, token);
}
await page.evaluate(INJECT_TOKEN, token);
await page.click('#recaptcha-demo-submit');
log.info('Token injected, form submitted');
},
],
The Python hook is the same walk with two differences that matter:
if token is None:
context.log.info("Solving the challenge for %s", pageurl)
# 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, pageurl
)
SOLVED[pageurl] = token
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. The Node client
is callback-based, so its promise slots into the async hook without
extra work.
Setup
# Node.js
npm install deathbycaptcha-lib crawlee
# Python
pip install deathbycaptcha-official
pip install -r examples/crawlee/requirements.txt
export DBC_USERNAME=your_username
export DBC_PASSWORD=your_password
# or: export DBC_AUTHTOKEN=your_authtoken
The Node example runs on Crawlee v3 (version 4 requires Node 22.15 and the repository supports Node 20); the Python example runs on Crawlee v1 for Python 3.10 or newer, and needs Playwright's Chromium once:
playwright install chromium
Run
node examples/crawlee/crawler.js
python examples/crawlee/crawler.py
The crawler writes its storage (datasets, request queues) under the
current working directory; point CRAWLEE_STORAGE_DIR somewhere else to
keep it out of a project.
The two balance guards
A crawl retries failed requests — Crawlee does it three times by default — and every retry would buy the same solve again. The example closes both doors:
- One token per URL. The solved map is keyed by page: a retried request re-injects the token it already bought instead of paying for the same solve twice.
max_requestRetries: 1/max_request_retries=1. A failed solve must not burn the balance on three more attempts.
The sandbox that stops CI
The first continuous-integration run of the Python example failed with "Chromium sandboxing failed": the JavaScript crawler turns the Chromium sandbox off by default, and the Python crawler leaves it on, which a CI container rarely supports. The example launches the same way as the JavaScript one:
browser_launch_options={"chromium_sandbox": False},
If your own runner has a working sandbox, delete the line and keep the protection.
What the crawler does
- Visits Google's reCAPTCHA demo — the vendor's own page, not a third party's production site.
- The pre-navigation hook blocks the static assets.
- The post-navigation hook reads the
data-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 Playwright, Puppeteer, Cypress and WebdriverIO samples in the Node client, and the Selenium and Playwright samples in the Python one. - Submits and waits for
.recaptcha-success.
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. Without them the token is solved from your crawler's
address and the site rejects it, which looks like a DeathByCaptcha
failure and is not one.
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. - HTTP crawlers cannot inject.
HttpCrawlerhas no page: the token is handed to the site as a parameter or a header instead of written into the widget. This example usesPlaywrightCrawlerbecause the widget walk is the part readers need to see. - Never print the token: the solvers return it as a value and nothing else should echo it.
Tests
npm run test:unit # solver tests, no network
DBC_USERNAME=... DBC_PASSWORD=... npm run test:integration:crawlee
python -m pytest tests/test_crawlee_solver.py -v # no network
DBC_TEST_USERNAME=... DBC_TEST_PASSWORD=... python -m pytest tests/test_crawlee_integration.py -v
The unit tests replace the client with a fake. The integration tests run the real crawler in a temporary directory against the live API, check that it reported a verified page and fail if anything in the output looks like a token. Both skip themselves when the credentials are not in the environment, and both run in the continuous integration of their repository on every push.
Next steps
- Node example: https://github.com/deathbycaptcha/deathbycaptcha-api-client-nodejs
- Python 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, 