# Puppeteer

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

Source: https://noptcha.com/docs/integrations/puppeteer/

[Puppeteer](https://pptr.dev/)’s `page.evaluate(fn, arg)` runs the [helper module](https://noptcha.com/docs/integrations.md#the-helper-modules)’s page functions; `solveOnPage` chains finding, solving and putting the token back.

## Setup

```sh
npm install puppeteer
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).

## Example

signup.mjs

```js
import puppeteer from 'puppeteer';
import { solveOnPage } from './noptcha-browser.mjs';

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

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(URL, { waitUntil: 'load' });
// ... fill in the form as usual ...

// find the captcha, solve it, put the token back (page.evaluate runs the helper's functions in the page)
const result = await solveOnPage((fn, arg) => page.evaluate(fn, arg));
if (result) console.log('solved', result.type, '->', result.did);

await Promise.all([page.waitForNavigation(), page.click('button[type=submit]')]); // your page's submit button
console.log(page.url(), await page.title());
await browser.close();
```

`solveOnPage` returns `null` when the page shows no captcha the helper recognizes, else `{ type, params, solution, did }`, where `did` lists the fields it filled and the callbacks it called. To do the steps yourself: `const [captcha] = await page.evaluate(findCaptchas)`, then `solve(captcha.type, captcha.params)`, then `page.evaluate(putToken, { type: captcha.type, token: solution.token })`.

## 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.** For a widget inside an iframe, use that frame’s `evaluate` (`page.frames()`) instead of the page’s.
-   **puppeteer-core.** With `puppeteer-core` and your own Chrome (`puppeteer.launch({ executablePath })` or `puppeteer.connect(...)`) the code is the same.

> **Note:** Tested with Puppeteer 25, against local pages that imitate each widget, with a test key.
