# Playwright (Node.js)

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

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

[Playwright](https://playwright.dev/) passes a function and an argument to `page.evaluate`, so the [helper module](https://noptcha.com/docs/integrations.md#the-helper-modules)’s `findCaptchas` and `putToken` run in the page as they are; `solve()` gets the token from the v1 API.

## Setup

```sh
npm install playwright
npx 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.mjs`](https://noptcha.com/docs/integrations.md#the-helper-modules) next to your script (Node.js 22 or later, ES modules; it uses the built-in `fetch`).

## Example

signup.mjs

```js
import { chromium } from 'playwright';
import { findCaptchas, putToken, solve } from './noptcha-browser.mjs';

const URL = 'https://example.com/signup'; // a page you are allowed to automate

const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto(URL);
// ... fill in the form as usual ...

const [captcha] = await page.evaluate(findCaptchas); // { type: 'recaptcha.v2', params: {...} }
if (captcha) {
  const solution = await solve(captcha.type, captcha.params);
  const did = await page.evaluate(putToken, { type: captcha.type, token: solution.token });
  console.log('solved', captcha.type, '->', did);
}

const navigated = page.waitForEvent('framenavigated', (frame) => frame === page.mainFrame());
await page.click('button[type=submit]'); // your page's submit button
await navigated;
await page.waitForLoadState();
console.log(page.url(), await page.title());
await browser.close();
```

Or in one call: `await solveOnPage((fn, arg) => page.evaluate(fn, arg))` finds the first captcha, solves it, puts the token back and returns `{ type, params, solution, did }` (`null` when the page has none).

## Notes

-   **Timing.** Find the captcha once the widget is on the page and solve right before you submit: tokens expire after a couple of minutes.
-   **Frames.** If the form and the widget are inside an iframe, evaluate in that frame (`page.frames()`, `frame.evaluate(findCaptchas)`).
-   **Test runner.** In `@playwright/test`, the same calls work inside a test; give the test a timeout that leaves room for solving (`test.setTimeout(180_000)`).
-   **reCAPTCHA v3.** `putToken` makes `grecaptcha.execute()` resolve with the token, so the site’s own submit handler picks it up: click the submit button afterwards.

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