You have a Playwright script that hits a login form gated by **reCAPTCHA v2**. Instead of clicking image tiles by hand, this recipe asks CaptchaSonic for a fresh `g-recaptcha-response` token and injects it into the page, so the form submits as if a human solved the challenge.

> [!TIP]
> This recipe uses the **token** flow (no image scraping). The SDK polls our infrastructure and returns a ready-to-submit token in 6–12 s on average.

---

## What you'll build

A Playwright script that opens a target page, solves its reCAPTCHA v2 via CaptchaSonic, injects the token, and submits the form — fully headless, no human interaction.

**Time:** ~5 minutes · **Difficulty:** beginner.

---

## Setup

```bash
pip install captchasonic playwright
playwright install chromium
export CAPTCHASONIC_API_KEY=sonic_xxx   # from /app
```

You need:
- A CaptchaSonic API key with credits ([get one](/app), then [add funds](/docs/guides/add-funds)).
- The target page's `website_url` and `website_key` (the reCAPTCHA sitekey, found in the page's HTML as `data-sitekey="…"`).

---

## The recipe

<LangTabs>
  <LangTabs.Tab label="Python">

```python
import os
from playwright.sync_api import sync_playwright
from captchasonic import CaptchaSonic

TARGET = "https://example.com/login"
SITEKEY = "6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-"

solver = CaptchaSonic(os.environ["CAPTCHASONIC_API_KEY"])

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True)
    page = browser.new_page()
    page.goto(TARGET)

    # 1. Ask CaptchaSonic for a token (auto-polls, returns when ready).
    result = solver.solve_recaptcha_v2_token(
        website_url=TARGET,
        website_key=SITEKEY,
    )
    token = result["token"]

    # 2. Inject the token into the page's g-recaptcha-response field.
    #    The textarea is hidden by default — make it visible-to-DOM first.
    page.evaluate(
        """(t) => {
            const el = document.getElementById('g-recaptcha-response');
            el.style.display = 'block';
            el.value = t;
        }""",
        token,
    )

    # 3. Submit the form.
    page.fill("input[name='username']", "you@example.com")
    page.fill("input[name='password']", "hunter2")
    page.click("button[type='submit']")
    page.wait_for_url("**/dashboard", timeout=15_000)
    print("logged in:", page.url)
    browser.close()
```

  </LangTabs.Tab>
  <LangTabs.Tab label="Node">

```javascript

const TARGET = "https://example.com/login";
const SITEKEY = "6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-";

const solver = new CaptchaSonic(process.env.CAPTCHASONIC_API_KEY);

const browser = await chromium.launch({ headless: true });
const page = await browser.newPage();
await page.goto(TARGET);

const result = await solver.solveRecaptchaV2Token({
  websiteURL: TARGET,
  websiteKey: SITEKEY,
});
const token = result.solution.gRecaptchaResponse;

await page.evaluate((t) => {
  const el = document.getElementById("g-recaptcha-response");
  el.style.display = "block";
  el.value = t;
}, token);

await page.fill("input[name='username']", "you@example.com");
await page.fill("input[name='password']", "hunter2");
await page.click("button[type='submit']");
await page.waitForURL("**/dashboard", { timeout: 15_000 });
console.log("logged in:", page.url());
await browser.close();
```

  </LangTabs.Tab>
</LangTabs>

> Returns a `g-recaptcha-response` token you write into the hidden textarea — the form's submit handler reads it directly, exactly as if a human had solved the challenge.

---

## Common pitfalls

- **`g-recaptcha-response` is missing from the page.** Some sites bind the token via a JavaScript callback (`data-callback="…"`). Call that function with the token instead of (or in addition to) writing the textarea — e.g. `page.evaluate("window.onCaptchaSolved(t)", token)`.
- **Token expires before submit.** reCAPTCHA tokens live ~120 s. Solve as late as possible in your script — right before `click('submit')`, not at script start.
- **Sitekey changes per environment.** Read it from the page at runtime: `page.get_attribute(".g-recaptcha", "data-sitekey")` rather than hard-coding.
- **Need geo-locked solving?** Pass `proxy="http://user:pass@host:port"` to `solve_recaptcha_v2_token` so the token is solved through *your* IP — required when the target validates IP↔token coherence.

---

## See also

- [Python SDK — reCAPTCHA v2 (token)](/docs/clients-sdks/python#recaptcha-v2-token)
- [Capability Matrix](/docs/clients-sdks/capabilities) — which return field for which captcha type
- [Error Handling](/docs/clients-sdks/python#error-handling) — `InsufficientBalanceError`, `MinuteLimitExceededError`, retry behavior
