Skip to content

Playwright (Node.js)

Playwright passes a function and an argument to page.evaluate, so the helper module’s findCaptchas and putToken run in the page as they are; solve() gets the token from the v1 API.

Terminal window
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 next to your script (Node.js 22 or later, ES modules; it uses the built-in fetch).

signup.mjs
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).

  • 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.