Skip to main content

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.

Python 3.9+ Patchright MIT License


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.

Peak
$0.90 per 1,000 Turnstile solves, down to $0.35 at volume · pay only on success · 1,000 free solves · docs · pricing

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

Metadata

Release files for patchright-turnstile 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for patchright-turnstile 0.1.1
File Size Uploaded Provenance Actions
patchright_turnstile-0.1.1.tar.gz 10.9 kB Without provenance Details for patchright_turnstile-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for patchright-turnstile 0.1.1
File Interpreter ABI Platform Actions
patchright_turnstile-0.1.1-py3-none-any.whl Python 3 none any Details for patchright_turnstile-0.1.1-py3-none-any.whl

Total release size: 20.8 kB

Release files / patchright_turnstile-0.1.1.tar.gz

Download URL patchright_turnstile-0.1.1.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
dd350fee91b613c6fee77817e4526ad9e31684ac15e92b25a65d4c997d7a0611
BLAKE2b-256 checksum
How to use checksums
64672363a478a5f261c43d85ade367e6c14b640830cfa5cefa1b746cd76b2522
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.0

Release files / patchright_turnstile-0.1.1-py3-none-any.whl

Download URL patchright_turnstile-0.1.1-py3-none-any.whl
Size 9.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
65a3894a83bb0224b79b175aaa4f26d8a785b08c6dd33743f40b1b4c119650c3
BLAKE2b-256 checksum
How to use checksums
5b7ea5db75c625c1517492a3155feb650835c80fc0613331e6ffec7ff7d89981
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.0

Security for patchright-turnstile 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page