# Stagehand

> Solve reCAPTCHA, hCaptcha, Turnstile and FunCaptcha in a Stagehand script with NoptCHA - run the helper with page.evaluate between act() steps, then submit.

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

[Stagehand](https://docs.stagehand.dev/) 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](https://noptcha.com/docs/integrations.md#the-helper-modules)’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.

## Setup

```sh
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`](https://noptcha.com/docs/integrations.md#the-helper-modules) next to your script (Node.js 22 or later, ES modules). Local runs need Chrome installed, or `executablePath` in `localBrowser.launch()`.

## Example

signup.mjs

```js
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.

## Notes

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

> **Note:** Tested with Stagehand 4.5 and a local Chrome against local pages that imitate each widget, with a test key. The `act()` step needs a model and was left out of the test.
