patchright-turnstile
Solve Cloudflare Turnstile in a Patchright page. Read the sitekey, get a token from Peak, put it back in the widget. One call, async or sync.
Patchright is a patched Playwright that removes the CDP leaks (Runtime.enable and friends) detectors look for. That gets you through a lot of bot checks. It does not answer a Turnstile challenge, because the token comes from Cloudflare's risk engine and not from a click. This package hands the (url, sitekey) to a solver and writes the returned token back into the page. Your cookies, TLS and session stay in your own browser.
from patchright_turnstile import solve_turnstile
token = await solve_turnstile(page, api_key="pk_your_api_key")
Install
pip install patchright-turnstile patchright
patchright install chromium
The package itself has no runtime dependencies.
Use it
import asyncio, os
from patchright.async_api import async_playwright
from patchright_turnstile import solve_turnstile
async def main():
async with async_playwright() as p:
browser = await p.chromium.launch_persistent_context(
"./profile", channel="chrome", headless=False, no_viewport=True
)
page = browser.pages[0]
await page.goto("https://site-with-turnstile.example/login")
await solve_turnstile(page, os.environ["PEAK_API_KEY"])
await page.click("button[type=submit]")
await browser.close()
asyncio.run(main())
Sync API:
from patchright_turnstile import solve_turnstile_sync
solve_turnstile_sync(page, os.environ["PEAK_API_KEY"])
solve_turnstile finds the sitekey (data-sitekey attribute or the challenges.cloudflare.com iframe), requests a token, fills every cf-turnstile-response field, and calls the widget's data-callback if the page set one.
The isolated-context catch
Patchright runs page.evaluate() in an isolated JavaScript context by default. In that context window.yourCallback does not exist, so a plain Playwright injection script can fill the hidden input and still fail to fire the page's success handler. This package passes isolated_context=False for the injection step, so the callback runs in the page's own world. If you inject tokens yourself, do the same:
await page.evaluate("window.onTurnstileOk('TOKEN')", isolated_context=False)
Matching the exit IP
Turnstile scores the IP that submits the token. If your browser goes through a proxy, give the solver the same one:
await solve_turnstile(page, api_key, proxy="http://user:pass@host:port")
Leave proxy off and the token is minted from a clean pool. That works on many sites; test yours.
API
await solve_turnstile(page, api_key=None, proxy=None, sitekey=None, timeout=180.0) returns the token string.
api_key: Peak key. Falls back to the PEAK_API_KEY environment variable.
sitekey: skip DOM detection and use this one.
timeout: seconds to wait for the solver.
solve_turnstile_sync takes the same arguments for Patchright's sync API. Failures raise PeakError.
Troubleshooting
Every failure raises PeakError with a string you can grep for. The two cases at the bottom raise nothing, which makes them harder to spot.
PeakError: No Turnstile sitekey found on the page. Is the widget loaded? The call ran before the widget rendered, or the site builds the widget from script inside a closed shadow root, where neither a data-sitekey attribute nor the iframe is reachable. Wait for the challenges.cloudflare.com iframe first, or pass sitekey=. How to find a Cloudflare Turnstile sitekey shows where sites hide it.
PeakError: No Peak API key. Neither api_key= nor PEAK_API_KEY held a key. Keys start with pk_; the free one comes from the dashboard.
PeakError: Peak HTTP <status>: ... Peak rejected the request before solving. The text after the colon is the response body with Peak's own reason, for example a key it does not recognise or an empty balance.
PeakError: Could not reach Peak: ... The HTTPS call to api.peak.fo failed on your side: DNS, TLS, a firewall, or a timeout. The proxy= argument is forwarded to Peak for the solve and is not used for this call.
PeakError: Peak solve failed: ... Peak answered with success: false. The usual causes are a sitekey that does not belong to the page URL, or a proxy= Peak could not connect through. The Cloudflare Turnstile error codes post covers the widget-side codes (110200, 300030, 600010 and the rest) that sit behind many failed solves.
- The token came back and the form still does nothing. The hidden input is filled, but the page waits on a JavaScript handler this package could not reach. It calls the function named in
data-callback; a handler passed to turnstile.render({callback}) as a closure has no global name to call. Find the handler in the page source and call it yourself with isolated_context=False, as in the section above. The cf-turnstile-response token explained and cf-turnstile-response is empty cover both halves.
- The token is accepted, then the next request returns 403. A Turnstile token proves one widget pass. A site that also runs the managed challenge wants a
cf_clearance cookie tied to your IP and User-Agent, and a token does not give you one. Turnstile token vs cookie explains which artefact a page wants, Cloudflare Turnstile 403 walks through the fix, and the Cloudflare 5-second challenge explained covers the interstitial.
Two longer reads for this stack: How to scrape Cloudflare-protected sites without getting blocked (also on Hashnode) and the 2026 Turnstile developer guide.
Getting a key
The solver behind this package is Peak. You pay only for solves that succeed, and the 1,000 free solves are enough to wire this up and test it. Peak also handles the Cloudflare 5-second challenge (task_type: "cloudflare5stask"); this package covers Turnstile.
$0.90 per 1,000 Turnstile solves, down to $0.35 at volume · pay only on success · 1,000 free solves · docs · pricing
Related
Same pattern for other stacks: cloudscraper-turnstile (a requests.Session drop-in, no browser) · turnstile-curl (the curl_cffi companion, no browser) · scrapy-turnstile · scrapling-turnstile · playwright-turnstile · camoufox-turnstile · nodriver-turnstile · pydoll-turnstile · drissionpage-turnstile · selenium-turnstile · botasaurus-turnstile · crawlee-turnstile · puppeteer-extra-plugin-turnstile · browser-use-turnstile · crawl4ai-turnstile. Full list in awesome-turnstile-solvers.
If you want to know what the widget does before it hands out a token, cloudflare-turnstile-reversed documents the challenge flow, the widget parameters and the fingerprinting it runs.
Notes
Use this on sites and accounts you're allowed to automate. Respect each target's Terms of Service and robots.txt. It is meant for automation, testing and public-data scraping, not credential stuffing or abuse. Not affiliated with Cloudflare or the Patchright project.
License
MIT