Skip to main content

drissionpage-turnstile

Solve Cloudflare Turnstile inside a DrissionPage browser.

DrissionPage drives a real Chromium, so when a page throws a Turnstile widget, the browser often clears it on its own. This library leans on that: it waits a few seconds for the browser to earn its own token, reads that token when it appears, and only calls a solver when the widget stalls. You pay for a solve exactly when the browser couldn't finish the job, not on every page.

When it does need a solver, it uses Peak, a Cloudflare Turnstile API: one call returns the token in about a second, and you're billed only for solves that land.

Install

pip install DrissionPage drissionpage-turnstile

Set your key once:

export PEAK_API_KEY=pk_your_key   # 1,000 free solves to start, no card

Use it

import os
from DrissionPage import ChromiumPage
from drissionpage_turnstile import solve_turnstile

page = ChromiumPage()
page.get("https://your-target.example/login")

result = solve_turnstile(page, os.environ["PEAK_API_KEY"])
# token is already in the page's cf-turnstile-response field
page.ele("tag:button@@text():Sign in").click()

print("free native solve" if result.native else f"paid solve, ${result.cost}")

That's the whole thing. solve_turnstile reads the sitekey off the page, reuses a token the browser already produced if there is one, otherwise asks Peak, drops the token into the widget's hidden input, and fires the page's callback so SPAs notice.

What it actually does

  1. Waits up to wait_native seconds (default 4) for the real browser to solve the widget itself. If a token shows up in the response field, it returns that (cost 0, native=True).
  2. If nothing appears, it reads the sitekey (from the data-sitekey div or the challenge iframe's src) and sends (url, sitekey) to Peak.
  3. It writes the returned token into every cf-turnstile-response field and calls the data-callback the page registered. Forms read the input on submit; single-page apps wait on the callback. This does both.

API

solve_turnstile(page, api_key, proxy=None, url=None, timeout=120.0, wait_native=4.0) -> SolveResult
read_sitekey(page, timeout=15.0, poll=0.5) -> str
request_token(api_key, url, sitekey, proxy=None, timeout=120.0) -> SolveResult

SolveResult has .token, .cost, .elapsed, and .native.

  • page: a DrissionPage ChromiumPage or WebPage.
  • proxy: pass the same proxy your browser is running behind. Turnstile tokens aren't always IP-bound, but when the target does bind them, a token solved from a different IP gets rejected. Matching the IP removes that failure. With no proxy, Peak uses its proxyless pool.
  • wait_native: set to 0 to skip the wait and solve immediately through Peak (useful when you already know the browser can't pass, e.g. headless on a flagged datacenter IP).

Notes

  • The token is single-use and expires in about five minutes, so solve close to when you submit.
  • If solve_turnstile raises "found nowhere to put it," the page renders the challenge but has no response field yet. Wait for the widget, or inject result.token where the form expects it.
  • This reads the sitekey already on the page; it doesn't start a challenge that isn't there.

Part of a set of Turnstile integrations for the common stacks: nodriver, camoufox, scrapling, playwright, selenium, crawlee, scrapy, cloudscraper, curl_cffi. Full list in awesome-turnstile-solvers.

License

MIT

Metadata

Release files for drissionpage-turnstile 0.1.0

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

Source distribution (sdist)

Source distribution for drissionpage-turnstile 0.1.0
File Size Uploaded
drissionpage_turnstile-0.1.0.tar.gz 6.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drissionpage-turnstile 0.1.0
File Interpreter ABI Platform
drissionpage_turnstile-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.5 kB

Release files / drissionpage_turnstile-0.1.0.tar.gz

Download URL drissionpage_turnstile-0.1.0.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
2a0dfe26fddf8df66e9f99d2091b9b5d90ce46f38892011fb26b060a84ff5077
BLAKE2b-256 checksum
How to use checksums
61335de1a91a0f317be8f6f4917a865b123da412b628eb7772fb97e19246a70d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.0

Release files / drissionpage_turnstile-0.1.0-py3-none-any.whl

Download URL drissionpage_turnstile-0.1.0-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3b2a6cad295e0a1e7616d6ebb6f219c73a6469c88b2792ad443e2154d55952ec
BLAKE2b-256 checksum
How to use checksums
432fb11bd3b5d84aaf75f0ff82ef28b9b771174b83185db18246592cbb58813d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.0

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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