# Playwright (Python)

> Solve reCAPTCHA, hCaptcha, Turnstile and FunCaptcha in a Playwright for Python script with NoptCHA - find the parameters, solve with the v1 API, put the token back and submit.

Source: https://noptcha.com/docs/integrations/playwright-python/

[Playwright](https://playwright.dev/python/) runs JavaScript in the page with `page.evaluate`, which is all the [helper module](https://noptcha.com/docs/integrations.md#the-helper-modules) needs: `FIND_JS` reads the captcha’s parameters, `solve()` gets the token from the v1 API, and `PUT_JS` puts it where the page reads it.

## Setup

```sh
pip install playwright requests
python -m playwright install chromium
export NOPTCHA_KEY=nop_test_...   # a test key: free, fixed answers
# no account yet? export NOPTCHA_KEY=keyless (keyless sandbox, same fixed answers)
```

Save [`noptcha_browser.py`](https://noptcha.com/docs/integrations.md#the-helper-modules) next to your script.

## Example

signup.py

```python
from playwright.sync_api import sync_playwright

from noptcha_browser import FIND_JS, PUT_JS, solve

URL = "https://example.com/signup"  # a page you are allowed to automate

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto(URL)
    # ... fill in the form as usual ...

    captchas = page.evaluate(FIND_JS)  # [{"type": "recaptcha.v2", "params": {...}}, ...]
    if captchas:
        captcha = captchas[0]
        solution = solve(captcha["type"], captcha["params"])
        did = page.evaluate(PUT_JS, {"type": captcha["type"], "token": solution["token"]})
        print("solved", captcha["type"], "->", did)

    with page.expect_event("framenavigated", lambda frame: frame == page.main_frame):
        page.click("button[type=submit]")  # your page's submit button
    page.wait_for_load_state()
    print(page.url, page.title())
    browser.close()
```

`page.evaluate(FIND_JS)` returns a list like `[{"type": "turnstile", "params": {"website_url": "…", "website_key": "…", "action": "signup"}}]`; the first entry goes to `solve()` unchanged. `PUT_JS` returns what it filled in and which callbacks it called, for example `["cf-turnstile-response", "callback onCaptcha"]`.

## Notes

-   **Timing.** Run `FIND_JS` once the widget is on the page (after `page.goto`, or after the step that shows the captcha), and solve right before you submit: tokens expire after a couple of minutes.
-   **Frames.** If the form and the widget are inside an iframe, call `frame.evaluate(...)` on that frame (`page.frame_locator(...)` / `page.frames`) instead of `page.evaluate`.
-   **Async API.** With `playwright.async_api`, `await page.evaluate(...)` the same strings and run `solve()` with `await asyncio.to_thread(solve, captcha["type"], captcha["params"])` so the event loop is not blocked.
-   **reCAPTCHA v3.** `PUT_JS` makes `grecaptcha.execute()` resolve with the token, so the site’s own submit handler picks it up: click the submit button afterwards, as in the example.

> **Note:** Tested with Playwright 1.65 for Python, against local pages that imitate each widget, with a test key.
