The act() method allows you to perform browser actions using natural language instructions. It uses AI to understand your intent, locate the appropriate elements on the page, and execute the action.
The action you want to perform, described in natural language (e.g., “click the login button”). Can also accept an Action object from observe() for cached/preview actions.
Returns a Promise<ActResult> with the following structure:
interface ActResult { success: boolean; // Whether the action succeeded message: string; // Descriptive message about the result actionDescription: string; // Description of what was attempted actions: Action[]; // Array of actions that were executed}
Each Action in the actions array contains:
interface Action { selector: string; // XPath selector used description: string; // Human-readable description method?: string; // Playwright method used (click, fill, etc.) arguments?: string[]; // Arguments passed to the method}
import { Stagehand } from "@stagehand/api";const stagehand = new Stagehand({ env: "BROWSERBASE", apiKey: process.env.BROWSERBASE_API_KEY,});await stagehand.init();const page = stagehand.context.pages()[0];await page.goto("https://example.com");// Click an elementawait stagehand.act("click the login button");// Fill a form fieldawait stagehand.act("type '[email protected]' in the email field");// Select from dropdownawait stagehand.act("select 'United States' from the country dropdown");
const page1 = stagehand.context.pages()[0];const page2 = await stagehand.context.newPage();await page2.goto("https://example.com/form");// Perform action on specific pageawait stagehand.act( "fill the name field with 'John Doe'", { page: page2 });