Skip to content

Stagehand

Stagehand mixes model-driven steps (act, observe, extract) with ordinary page code. It has no plug-in point for a captcha service, but its page.evaluate(fn, arg) runs the helper module’s page functions as they are, so solve the captcha from your code between two act() steps.

This is for a local browser (localBrowser.launch()). Browsers run on Browserbase can solve captchas on their own.

Terminal window
npm install @browserbasehq/stagehand
export NOPTCHA_KEY=nop_test_... # a test key: free, fixed answers
# no account yet? export NOPTCHA_KEY=keyless (keyless sandbox, same fixed answers)
export OPENAI_API_KEY=... # or the key of the model you use

Save noptcha-browser.mjs next to your script (Node.js 22 or later, ES modules). Local runs need Chrome installed, or executablePath in localBrowser.launch().

signup.mjs
import { localBrowser, Stagehand } from '@browserbasehq/stagehand';
import { solveOnPage } from './noptcha-browser.mjs';
const URL = 'https://example.com/signup'; // a page you are allowed to automate
const browser = await localBrowser.launch({ headless: true });
const model = { modelName: 'openai/gpt-5.4-mini', apiKey: process.env.OPENAI_API_KEY }; // any model Stagehand supports
const stagehand = await Stagehand.create({ browser, model });
const [page] = await browser.context.pages();
await page.goto(URL);
await stagehand.act('fill in the sign-up form with the email test@example.com');
// page.evaluate(fn, arg) runs the helper's functions in the page, as in Playwright
const result = await solveOnPage((fn, arg) => page.evaluate(fn, arg));
if (result) console.log('solved', result.type, '->', result.did);
// submit, then wait until the next page has loaded (the mark is gone with the old document)
await page.evaluate(() => (window.beforeSubmit = true));
await page.locator('button[type=submit]').click(); // or let the model do it with act()
await page.waitForFunction(() => !window.beforeSubmit && document.readyState === 'complete');
console.log(await page.url());
await stagehand.close();
await browser.close();

solveOnPage returns null when the page shows no captcha the helper recognizes, else { type, params, solution, did }. The model never sees the key or the token.

  • Timing. Solve after the step that shows the captcha and right before you submit: tokens expire after a couple of minutes.
  • Submitting. locator(...).click() returns once the click is done, not when the next page has loaded; the example marks the old page and waits until it is gone.
  • Stagehand 3. In Stagehand 3 the page is stagehand.context.pages()[0]; page.evaluate(fn, arg) works the same.