On this page
A scraper fills in a form, and then a Cloudflare Turnstile box appears above the submit button and the script can’t go further. Turnstile rarely asks for anything visual. It runs checks on the browser and sometimes shows a checkbox, and stock Playwright tends to fail those checks.
This post is about the widget inside a page. The full-page “Just a moment…” challenge is covered in the Cloudflare bypass guide, with the same library.
Setup
The examples use playwright-captcha, a library I wrote for this. Python 3.10 or newer is required by the current Playwright.
pip install playwright playwright-captcha
playwright install chromium
There are two ways to solve the widget. The click solver clicks the checkbox in your browser for free, and only works if Cloudflare believes the browser. The 2Captcha solver buys a token from the service and puts it into the page; 2Captcha listed Turnstile at €1.40 per 1,000 solves in December 2026.
Try clicking first and pay only if that fails on your site.
Method 1: click solver
The first example uses stock Playwright so the structure is easy to see. On real sites it is often detected, and the Patchright and Camoufox versions below are the ones to use.
import asyncio
import logging
from playwright.async_api import async_playwright
from playwright_captcha import CaptchaType, ClickSolver, FrameworkType
logging.basicConfig(level='INFO')
async def solve_turnstile():
async with async_playwright() as playwright:
browser = await playwright.chromium.launch(headless=False)
page = await browser.new_page()
framework = FrameworkType.PLAYWRIGHT
# Create the solver BEFORE navigating: it needs to attach early
async with ClickSolver(framework=framework, page=page) as solver:
await page.goto('https://2captcha.com/demo/cloudflare-turnstile')
await asyncio.sleep(5)
turnstile_container = page.locator('#cf-turnstile')
await turnstile_container.wait_for()
await solver.solve_captcha(
captcha_container=page,
captcha_type=CaptchaType.CLOUDFLARE_TURNSTILE
)
submit_button = page.locator('//button[@data-action="demo_action"]')
await submit_button.wait_for()
await submit_button.click()
await asyncio.sleep(5)
try:
await page.locator('//p[text()="Captcha is passed successfully!"]').wait_for(timeout=10000)
logging.info('Solved!')
except Exception as e:
logging.error(f'Failed: {e}')
asyncio.run(solve_turnstile())
Open the solver before page.goto(). On start it adds a script to the page that opens closed shadow roots, and the Turnstile checkbox sits inside one. Added after navigation, the script misses the widget on that page.
With Patchright
Patchright changes Chrome and the driver instead of injecting scripts into the page. Here you change the import and set FrameworkType.PATCHRIGHT.
pip install patchright
patchright install chromium
import asyncio
import logging
from patchright.async_api import async_playwright
from playwright_captcha import CaptchaType, ClickSolver, FrameworkType
logging.basicConfig(level='INFO')
async def solve_turnstile_patchright():
async with async_playwright() as playwright:
browser = await playwright.chromium.launch(channel="chrome", headless=False)
context = await browser.new_context()
page = await context.new_page()
framework = FrameworkType.PATCHRIGHT
async with ClickSolver(framework=framework, page=page) as solver:
await page.goto('https://2captcha.com/demo/cloudflare-turnstile')
await asyncio.sleep(5)
turnstile_container = page.locator('#cf-turnstile')
await turnstile_container.wait_for()
await solver.solve_captcha(
captcha_container=page,
captcha_type=CaptchaType.CLOUDFLARE_TURNSTILE
)
submit_button = page.locator('//button[@data-action="demo_action"]')
await submit_button.wait_for()
await submit_button.click()
await asyncio.sleep(5)
try:
await page.locator('//p[text()="Captcha is passed successfully!"]').wait_for(timeout=10000)
logging.info('Solved!')
except Exception as e:
logging.error(f'Failed: {e}')
asyncio.run(solve_turnstile_patchright())
With Camoufox
Camoufox is a Firefox build with a faked, consistent fingerprint. add_init_script doesn’t work properly in it, so playwright-captcha ships an addon that does the same job. Turn it on with main_world_eval=True and the addon path:
pip install camoufox
camoufox fetch
import asyncio
import logging
import os
from camoufox import AsyncCamoufox
from playwright_captcha.utils.camoufox_add_init_script.add_init_script import get_addon_path
from playwright_captcha import CaptchaType, ClickSolver, FrameworkType
logging.basicConfig(level='INFO')
ADDON_PATH = get_addon_path()
async def solve_turnstile_camoufox():
async with AsyncCamoufox(
headless=False,
geoip=True,
humanize=True,
main_world_eval=True,
addons=[os.path.abspath(ADDON_PATH)]
) as browser:
context = await browser.new_context()
page = await context.new_page()
framework = FrameworkType.CAMOUFOX
async with ClickSolver(framework=framework, page=page) as solver:
await page.goto('https://2captcha.com/demo/cloudflare-turnstile')
await asyncio.sleep(5)
turnstile_container = page.locator('#cf-turnstile')
await turnstile_container.wait_for()
await solver.solve_captcha(
captcha_container=page,
captcha_type=CaptchaType.CLOUDFLARE_TURNSTILE
)
submit_button = page.locator('//button[@data-action="demo_action"]')
await submit_button.wait_for()
await submit_button.click()
await asyncio.sleep(5)
try:
await page.locator('//p[text()="Captcha is passed successfully!"]').wait_for(timeout=10000)
logging.info('Solved!')
except Exception as e:
logging.error(f'Failed: {e}')
asyncio.run(solve_turnstile_camoufox())
Method 2: 2Captcha API
The solver reads the sitekey from the page, sends it to 2Captcha with the page URL, waits for a token and puts it into the form. Get an API key at 2captcha.com and put it in .env:
TWO_CAPTCHA_API_KEY=your_api_key_here
playwright-captcha already installs an async 2Captcha client, so add only python-dotenv. Don’t install the official 2captcha-python as well: both provide the twocaptcha module and will overwrite each other.
pip install python-dotenv
import asyncio
import logging
import os
from dotenv import load_dotenv
from playwright.async_api import async_playwright
from twocaptcha import AsyncTwoCaptcha
from playwright_captcha import CaptchaType, TwoCaptchaSolver, FrameworkType
logging.basicConfig(level='INFO')
load_dotenv()
TWO_CAPTCHA_API_KEY = os.getenv('TWO_CAPTCHA_API_KEY')
async def solve_turnstile_api():
async with async_playwright() as playwright:
browser = await playwright.chromium.launch(headless=False)
context = await browser.new_context()
page = await context.new_page()
framework = FrameworkType.PLAYWRIGHT
async with TwoCaptchaSolver(
framework=framework,
page=page,
async_two_captcha_client=AsyncTwoCaptcha(TWO_CAPTCHA_API_KEY)
) as solver:
await page.goto('https://2captcha.com/demo/cloudflare-turnstile')
await asyncio.sleep(5)
turnstile_container = page.locator('#cf-turnstile')
await turnstile_container.wait_for()
await solver.solve_captcha(
captcha_container=turnstile_container,
captcha_type=CaptchaType.CLOUDFLARE_TURNSTILE,
)
submit_button = page.locator('//button[@data-action="demo_action"]')
await submit_button.wait_for()
await submit_button.click()
await asyncio.sleep(5)
try:
await page.locator('//p[text()="Captcha is passed successfully!"]').wait_for(timeout=10000)
logging.info('Solved!')
except Exception as e:
logging.error(f'Failed: {e}')
asyncio.run(solve_turnstile_api())
Here the solver gets turnstile_container instead of the whole page. It looks for the element with data-sitekey inside the container you pass.
Which method to use
| Click solver | 2Captcha API | |
|---|---|---|
| Cost | Free | €1.40 per 1,000 solves (Dec 2026) |
| Outside service | No | Yes, you wait for its answer |
| Depends on the browser’s disguise | A lot | Less |
With Patchright or Camoufox already in place, start with the click solver. Move to 2Captcha when clicks keep failing and the cost per solve is fine for your volume.
Retries
Both solvers retry, 3 times with 5 seconds between attempts by default:
# Click solver: more retries for tricky sites
solver = ClickSolver(
framework=framework,
page=page,
max_attempts=5,
attempt_delay=3
)
# API solver: longer delays for slow services
solver = TwoCaptchaSolver(
framework=framework,
page=page,
async_two_captcha_client=AsyncTwoCaptcha(TWO_CAPTCHA_API_KEY),
max_attempts=3,
attempt_delay=10,
)
Common problems
Turnstile keeps failing on stock Playwright. Cloudflare has recognised the browser before you clicked anything. Use the Patchright or Camoufox version.
Camoufox throws an add_init_script error. main_world_eval=True or the addon path is missing where you create the browser.
The sitekey isn’t detected. Find data-sitekey in the widget’s HTML and pass it yourself: solver.solve_captcha(..., sitekey='...').
Patchright still gets blocked. Check the IP. If Cloudflare already distrusts the address, a better browser won’t change the result. Try a different proxy, preferably residential.
The library
playwright-captcha is open source. Besides Turnstile it solves the Cloudflare full-page challenge and reCAPTCHA v2 and v3, with 2Captcha or 10Captcha as the paid service.