How to Bypass Cloudflare in 2026 with Python and Playwright (Full Guide)

UNDETECTED LABS6 MIN READ
On this page

When a scraper hits a Cloudflare-protected site, there is usually no picture puzzle to solve. Cloudflare checks the browser in the background and then either lets the request through or holds it on a challenge. This guide shows how to get past that challenge from Python and Playwright with playwright-captcha, an open-source library I wrote: either by clicking the checkbox yourself or by paying 2Captcha to return a token.

Two kinds of Cloudflare challenge

The Turnstile widget sits inside a page, usually in a form. Sometimes it passes on its own, sometimes it shows a checkbox.

The interstitial is a full-page challenge (“Just a moment…”) shown before the site itself. The real page only loads once the browser passes it.

playwright-captcha handles both.

Setup

You need Python 3.10 or newer (the current Playwright requires it).

pip install playwright playwright-captcha
playwright install chromium

Click or API

The click solver finds the Turnstile checkbox and clicks it in the browser. It costs nothing and needs no outside service, but Cloudflare still checks the browser, so stock Playwright often fails here. It works much better with a stealth browser like Camoufox or Patchright.

The API solver sends the sitekey and page URL to 2Captcha, waits for a token and puts it into the page. It depends less on how good your browser’s disguise is, but every solve costs money: 2Captcha listed Turnstile at €1.40 per 1,000 solves in December 2026. You also wait for the service to answer.

Start with the click solver. If it keeps failing on your target, try the API.

Method 1: click solver

Stock Playwright is often detected by Cloudflare, so for real targets use Camoufox, Patchright or an anti-detect browser (setup for the first two is further down). The examples here use plain Playwright to keep them short.

Turnstile widget

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 solver BEFORE navigating to the page
        async with ClickSolver(framework=framework, page=page) as solver:
            await page.goto('https://2captcha.com/demo/cloudflare-turnstile')
            await asyncio.sleep(5)  # wait for page to load

            # Find the Turnstile container
            turnstile_container = page.locator('#cf-turnstile')
            await turnstile_container.wait_for()

            # Solve it
            await solver.solve_captcha(
                captcha_container=page,
                captcha_type=CaptchaType.CLOUDFLARE_TURNSTILE
            )

        # Submit the form
        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('Captcha solved successfully!')
        except Exception as e:
            logging.error(f'Failed: {e}')

asyncio.run(solve_turnstile())

Full-page challenge

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_interstitial():
    async with async_playwright() as playwright:
        browser = await playwright.chromium.launch(headless=False)
        page = await browser.new_page()

        framework = FrameworkType.PLAYWRIGHT

        async with ClickSolver(framework=framework, page=page) as solver:
            await page.goto('https://2captcha.com/demo/cloudflare-turnstile-challenge')
            await asyncio.sleep(5)

            await solver.solve_captcha(
                captcha_container=page,
                captcha_type=CaptchaType.CLOUDFLARE_INTERSTITIAL,
                expected_content_selector="#root"  # selector that appears after the challenge passes
            )

            await asyncio.sleep(5)

        try:
            await page.locator('//p[text()="Captcha is passed successfully!"]').wait_for(timeout=10000)
            logging.info('Interstitial solved successfully!')
        except Exception as e:
            logging.error(f'Failed: {e}')

asyncio.run(solve_interstitial())

expected_content_selector is an element that only exists on the real page. If it is already visible, for example because Patchright passed the check on its own, the solver doesn’t click anything and returns right away.

Method 2: 2Captcha API

2Captcha solves the challenge on its side and sends back a token, which the solver puts into the page.

Put your API key in a .env file:

# .env file
TWO_CAPTCHA_API_KEY=your_api_key_here

The async 2Captcha client comes with playwright-captcha as a dependency, so only python-dotenv is left to install. Don’t install the official 2captcha-python next to it: both packages provide the same twocaptcha module.

pip install python-dotenv

Turnstile widget

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('Captcha solved successfully!')
        except Exception as e:
            logging.error(f'Failed: {e}')

asyncio.run(solve_turnstile_api())

Full-page challenge

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_interstitial_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-challenge',
                wait_until='domcontentloaded'
            )

            await asyncio.sleep(15)  # interstitial needs more time to load

            await solver.solve_captcha(
                captcha_container=page,
                captcha_type=CaptchaType.CLOUDFLARE_INTERSTITIAL,
            )

        await asyncio.sleep(5)

        try:
            await page.locator('//p[text()="Captcha is passed successfully!"]').wait_for(timeout=10000)
            logging.info('Interstitial solved successfully!')
        except Exception as e:
            logging.error(f'Failed: {e}')

asyncio.run(solve_interstitial_api())

The interstitial takes longer to appear than the widget, hence the 15-second wait before solving.

Stealth browsers

The click solver works with any browser you can drive through Playwright, including anti-detect browsers such as AdsPower, Dolphin Anty, Multilogin or GoLogin. The library has built-in support for Patchright and Camoufox.

Patchright

Patchright is Playwright with patches to Chrome and the driver that hide automation. In my browser benchmark it was the only engine that passed all ten protected pages headed.

pip install patchright
patchright install chromium
from patchright.async_api import async_playwright
from playwright_captcha import FrameworkType

async with async_playwright() as playwright:
    browser = await playwright.chromium.launch(channel="chrome")
    context = await browser.new_context()
    page = await context.new_page()

    framework = FrameworkType.PATCHRIGHT
    # ... rest of your code

Camoufox

Camoufox is a Firefox build that fakes a consistent fingerprint: canvas, WebGL, fonts, screen, timezone and locale.

pip install camoufox
camoufox fetch
from camoufox import AsyncCamoufox
from playwright_captcha.utils.camoufox_add_init_script.add_init_script import get_addon_path
from playwright_captcha import FrameworkType
import os

ADDON_PATH = get_addon_path()

async with AsyncCamoufox(
    headless=False,
    geoip=True,
    humanize=True,
    main_world_eval=True,        # required for playwright-captcha
    addons=[os.path.abspath(ADDON_PATH)]  # required for playwright-captcha
) as browser:
    context = await browser.new_context()
    page = await context.new_page()

    framework = FrameworkType.CAMOUFOX
    # ... rest of your code

add_init_script doesn’t work properly in Camoufox, and the solver relies on it. playwright-captcha ships a small addon that does the same job. main_world_eval=True and the addon path above turn it on.

Solver options

Both solvers retry. By default they try 3 times with 5 seconds between attempts:

# Click solver options
solver = ClickSolver(
    framework=framework,
    page=page,
    max_attempts=5,     # retry up to 5 times
    attempt_delay=3     # wait 3 seconds between attempts
)

# API solver options
solver = TwoCaptchaSolver(
    framework=framework,
    page=page,
    async_two_captcha_client=captcha_client,
    max_attempts=3,
    attempt_delay=10,
)

# If the sitekey isn't found on the page, pass it when solving
await solver.solve_captcha(
    captcha_container=page,
    captcha_type=CaptchaType.CLOUDFLARE_TURNSTILE,
    sitekey='your_sitekey_here',
)

Which method to use

Click solver 2Captcha API
Cost Free €1.40 per 1,000 Turnstile solves (Dec 2026)
Outside service No Yes, you wait for its answer
Depends on the browser’s disguise A lot Less

If you already run Camoufox or Patchright, the click solver is free and usually enough. If it keeps failing on your target and the volume makes the cost acceptable, use 2Captcha.

Common problems

Turnstile keeps failing on stock Playwright. Cloudflare recognises the browser. Switch to Patchright or Camoufox.

Camoufox throws an add_init_script error. Pass main_world_eval=True and the addon path when you create the browser, as in the Camoufox example.

The sitekey isn’t detected. Find data-sitekey in the Turnstile widget’s HTML and pass it to solve_captcha(sitekey=...).

The library

playwright-captcha solves Cloudflare Turnstile (the widget and the full-page challenge) and reCAPTCHA v2 and v3. It works with Playwright, Patchright and Camoufox, and with 2Captcha or 10Captcha as the solving service.