Skip to content

Crawl4AI

Crawl4AI detects captchas but does not solve them. Its hooks get the Playwright page, so an after_goto hook can run the helper module: find the captcha, solve it with the v1 API, put the token back and submit, before Crawl4AI reads the page. (js_code in CrawlerRunConfig runs JavaScript too, but does not return its result, which finding the parameters needs.)

Terminal window
pip install crawl4ai requests
crawl4ai-setup # installs the browser
export NOPTCHA_KEY=nop_test_... # a test key: free, fixed answers
# no account yet? export NOPTCHA_KEY=keyless (keyless sandbox, same fixed answers)

Save noptcha_browser.py next to your script.

crawl.py
import asyncio
from crawl4ai import AsyncWebCrawler, BrowserConfig, CrawlerRunConfig
from noptcha_browser import FIND_JS, PUT_JS, solve
URL = "https://example.com/signup" # a page you are allowed to automate
async def after_goto(page, context, url, response, **kwargs):
"""Runs right after the page loaded (page is a Playwright page): solve a captcha if there is one, then submit."""
captchas = await page.evaluate(FIND_JS)
if captchas:
captcha = captchas[0]
solution = await asyncio.to_thread(solve, captcha["type"], captcha["params"]) # solve() blocks
did = await page.evaluate(PUT_JS, {"type": captcha["type"], "token": solution["token"]})
print("solved", captcha["type"], "->", did)
async with page.expect_event("framenavigated", lambda frame: frame == page.main_frame):
await page.click("button[type=submit]") # your page's submit button
await page.wait_for_load_state()
return page
async def main():
crawler = AsyncWebCrawler(config=BrowserConfig(headless=True))
crawler.crawler_strategy.set_hook("after_goto", after_goto)
await crawler.start()
try:
result = await crawler.arun(URL, config=CrawlerRunConfig())
print(result.success, result.url)
print(result.markdown[:500]) # the page after the form was submitted
finally:
await crawler.close()
asyncio.run(main())

The hook is async; solve() blocks, so it runs in a thread (asyncio.to_thread). A hook must return the page.

  • Only where needed. The hook runs for every arun(). On pages without a captcha FIND_JS returns an empty list and nothing else happens.
  • Sessions. With session_id, later arun(..., js_only=True) calls continue on the page the hook left, so you solve once per session.
  • After submitting. The example clicks the submit button and waits for the next page; Crawl4AI then returns that page (result.url, result.markdown).