Metadata-Version: 2.5
Name: selenium-turnstile
Version: 0.2.1
Summary: Solve Cloudflare Turnstile in Selenium via the Peak API, for when SeleniumBase UC-mode or undetected-chromedriver stall headless.
Project-URL: Homepage, https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=selenium-turnstile
Project-URL: Documentation, https://peak.fo/docs/turnstile?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=selenium-turnstile
Project-URL: Pricing, https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=selenium-turnstile#pricing
Project-URL: Source, https://github.com/CircuitSavage/selenium-turnstile
Author: Peak
License: MIT
License-File: LICENSE
Keywords: bypass,captcha,cloudflare,selenium,seleniumbase,solver,turnstile,undetected-chromedriver
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Internet :: WWW/HTTP :: Browsers
Classifier: Topic :: Software Development :: Testing
Requires-Python: >=3.8
Provides-Extra: selenium
Requires-Dist: selenium>=4.0; extra == 'selenium'
Description-Content-Type: text/markdown

<a href="https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=selenium-turnstile">
  <img src="https://raw.githubusercontent.com/CircuitSavage/selenium-turnstile/main/assets/peak-banner.png" alt="Peak: solve Cloudflare Turnstile and the 5-second challenge" width="100%">
</a>

# selenium-turnstile

**selenium-turnstile** is a Selenium helper that solves Cloudflare Turnstile by injecting a Peak-solved token, so a protected form or navigation proceeds without a real mouse click.

## Why

SeleniumBase's UC-mode click-solve (`uc_gui_click_captcha`) drives a physical click on the widget, which needs a real display and fails headless on datacenter IPs. undetected-chromedriver has no click-solve at all and gets stuck in the "Just a moment" loop, reloading the interstitial again and again.

This helper skips the click. It reads the sitekey from the DOM, gets a token from Peak, and injects that token as the `cf-turnstile-response` value and calls the widget's success callback. You need a Peak API key and one function call.

## Powered by Peak

This package uses [Peak](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=selenium-turnstile) to solve Turnstile.
- Solves Cloudflare Turnstile and the 5-second challenge
- $0.90 per 1,000 successful Turnstile solves, down to $0.35 at volume
- You pay only for solves that land. 1,000 free solves to start, no card.
→ [Get your free API key](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=selenium-turnstile) · [Docs](https://peak.fo/docs/turnstile?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=selenium-turnstile) · [Pricing](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=selenium-turnstile#pricing)

## Install

```bash
pip install selenium-turnstile selenium
```

## Quickstart

```python
import os
from selenium import webdriver
from selenium_turnstile import solve_turnstile

os.environ["PEAK_API_KEY"] = "pk_your_api_key"

driver = webdriver.Chrome()
driver.get("https://protected.example/login")

# Reads the sitekey from the page, solves via Peak, injects the token.
token = solve_turnstile(driver)

driver.find_element("css selector", "form").submit()
```

Works unchanged with a plain Selenium driver, a SeleniumBase `Driver`, or an undetected-chromedriver `Chrome`. All three expose the `current_url` / `find_element` / `execute_script` interface the helper uses.

## How it works

1. **Read the sitekey.** `find_element` looks for the `data-sitekey` attribute on the `.cf-turnstile` widget; if the sitekey is only set through a JS `render()` call, an `execute_script` DOM scan finds it.
2. **Solve via Peak.** `POST https://api.peak.fo/solve` with `task_type: "turnstiletask"`, the sitekey, and `url=driver.current_url`. Pass `proxy=` to mint the token from your browser's egress IP.
3. **Inject the token.** `execute_script` sets every `cf-turnstile-response` field, fires `input`/`change` events, and calls the widget's `data-callback` success handler so the form or navigation continues.

## API

```python
solve_turnstile(
    driver,               # any Selenium-like WebDriver
    api_key=None,         # defaults to env PEAK_API_KEY
    proxy=None,           # optional http://user:pass@ip:port, forwarded to Peak
    sitekey=None,         # override auto-detection (e.g. widget in a frame)
    url=None,             # defaults to driver.current_url
    task_type="turnstiletask",
    timeout=180.0,
    app_id=None,          # optional; earns you a share of each solve
) -> str                  # the injected token
```

Helpers `read_sitekey(driver)` and `inject_token(driver, token)` are exposed for finer control. Peak also supports `task_type="cloudflare5stask"` for the 5s interstitial challenge.

The API key is read from the `PEAK_API_KEY` environment variable by default; you can also pass `api_key=`. Never hardcode a real key.

## Earn with your app ID

Pass `app_id="app_your_id"` (or set the `PEAK_APP_ID` environment variable) to earn 5% of every solve routed through this package, paid as Peak solve credit. It is optional and does not change whether a solve succeeds or how fast it returns.

```python
solve_turnstile(driver, app_id="app_your_id")   # or: export PEAK_APP_ID=app_your_id
```

Create an app id at [peak.fo/dashboard/developer](https://peak.fo/dashboard/developer); details at [peak.fo/earn](https://peak.fo/earn).

## Troubleshooting

Every failure raises `PeakError`. The message tells you which step broke.

- **`PeakError: No Turnstile sitekey found on the page.`** `read_sitekey` looks once and does not wait. If the widget renders after load, wait for it first: `WebDriverWait(driver, 15).until(lambda d: d.find_elements("css selector", "[data-sitekey]"))`. A widget built by `turnstile.render()` inside a script bundle may leave no `data-sitekey` in the DOM. Copy the key from the network tab and pass `sitekey=`. [How to find a Cloudflare Turnstile sitekey](https://blog.peak.fo/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](https://peak.fo/?utm_source=github&utm_medium=readme&utm_campaign=packages&utm_content=selenium-turnstile).
- **`PeakError: Peak request failed: ...`** The request never reached Peak. Check DNS, the firewall, and any `HTTPS_PROXY` variable in the environment, since `urllib` honours it.
- **`PeakError` with Peak's own reason, or `Peak solve failed`.** Peak answered with `success: false`. Common causes: an empty balance, a sitekey that does not belong to the URL, or a `proxy=` Peak could not connect through. Peak does not bill a failed solve. The [Cloudflare Turnstile error codes](https://blog.peak.fo/cloudflare-turnstile-error-codes/) post covers the widget-side codes (110200, 300030, 600010 and the rest).
- **The token comes back and the submit button stays disabled.** The injector fills every `cf-turnstile-response` field and calls the function named in the widget's `data-callback` attribute. A site that passes a closure to `turnstile.render({callback: ...})` keeps that function out of reach. Submit the form yourself with `driver.execute_script("document.querySelector('form').submit()")`, or call the site's own handler with the token. [cf-turnstile-response is empty](https://blog.peak.fo/cf-turnstile-response-empty/) and [the cf-turnstile-response token explained](https://blog.peak.fo/the-cf-turnstile-response-token-explained/) cover the field.
- **The submit returns 403 or the form page again.** The site refused the token. A token is single-use and expires 300 seconds after Cloudflare issues it, so submit right away. Pass `proxy=` with the browser's own egress so Peak mints the token from the address that submits it. [Cloudflare Turnstile 403](https://blog.peak.fo/cloudflare-turnstile-403-scraper-fix/) covers the refusal.
- **`driver.get()` lands on "Just a moment..." and never leaves.** That page is the managed 5-second challenge, a separate check from the Turnstile widget. It wants a `cf_clearance` cookie, and this helper injects widget tokens. [Turnstile token vs cookie](https://blog.peak.fo/cloudflare-turnstile-token-vs-cookie/) explains the difference, and [the Cloudflare 5-second challenge explained](https://blog.peak.fo/the-cloudflare-5-second-challenge-explained/) covers the interstitial.

Two longer reads for this stack: [How to solve Cloudflare Turnstile in Selenium (Python)](https://blog.peak.fo/solve-cloudflare-turnstile-in-selenium-python/) and the [2026 Turnstile developer guide](https://blog.peak.fo/how-to-solve-cloudflare-turnstile-in-2026-dev-guide/).

## Related

Same pattern for other stacks: [playwright-turnstile](https://github.com/CircuitSavage/playwright-turnstile) · [patchright-turnstile](https://github.com/CircuitSavage/patchright-turnstile) · [nodriver-turnstile](https://github.com/CircuitSavage/nodriver-turnstile) · [camoufox-turnstile](https://github.com/CircuitSavage/camoufox-turnstile) · [pydoll-turnstile](https://github.com/CircuitSavage/pydoll-turnstile) · [drissionpage-turnstile](https://github.com/CircuitSavage/drissionpage-turnstile) · [botasaurus-turnstile](https://github.com/CircuitSavage/botasaurus-turnstile) · [scrapling-turnstile](https://github.com/CircuitSavage/scrapling-turnstile) · [scrapy-turnstile](https://github.com/CircuitSavage/scrapy-turnstile) · [cloudscraper-turnstile](https://github.com/CircuitSavage/cloudscraper-turnstile) (a `requests.Session` drop-in, no browser) · [turnstile-curl](https://github.com/CircuitSavage/turnstile-curl) · [crawlee-turnstile](https://github.com/CircuitSavage/crawlee-turnstile) · [puppeteer-extra-plugin-turnstile](https://github.com/CircuitSavage/puppeteer-extra-plugin-turnstile) · [browser-use-turnstile](https://github.com/CircuitSavage/browser-use-turnstile) · [crawl4ai-turnstile](https://github.com/CircuitSavage/crawl4ai-turnstile). Full list in [awesome-turnstile-solvers](https://github.com/CircuitSavage/awesome-turnstile-solvers).

[cloudflare-turnstile-reversed](https://github.com/CircuitSavage/cloudflare-turnstile-reversed) documents the challenge flow, the widget parameters and the fingerprinting the widget runs before it hands out a token.

## Legitimate use

For automation, QA, and scraping public data you are allowed to access. Respect each target's Terms of Service and robots directives, and do not use this for credential-stuffing or other abuse.

## License

MIT. See [LICENSE](./LICENSE).
