The act() method performs actions on the page using natural language instructions. It uses AI to understand your intent and execute the appropriate interactions.
import { Stagehand } from "@browserbasehq/stagehand";const stagehand = new Stagehand({ env: "LOCAL" });await stagehand.init();const page = await stagehand.context.newPage();await page.goto("https://example.com");// Click an elementawait stagehand.act("click the login button");// Fill a form fieldawait stagehand.act("type 'user@example.com' in the email field");// Select from dropdownawait stagehand.act("select 'United States' from the country dropdown");// Check a checkboxawait stagehand.act("check the 'Remember me' checkbox");await stagehand.close();
// Find elements firstconst actions = await stagehand.observe("buttons on the page");if (actions.length > 0) { // Perform action on the first button const result = await stagehand.act(actions[0]); console.log(result.message);}
// Use a different model for this actionawait stagehand.act("click the submit button", { model: "anthropic/claude-3-5-sonnet-latest",});// Or with client optionsawait stagehand.act("fill in the form", { model: { modelName: "openai/gpt-4o", temperature: 0.5, },});
await stagehand.act("click the submit button");await stagehand.act("click the first item in the list");await stagehand.act("double click the file icon");
await stagehand.act("type 'hello world' in the search box");await stagehand.act("clear the text field and type 'new value'");await stagehand.act("press Enter in the input field");
await stagehand.act("check the 'I agree' checkbox");await stagehand.act("uncheck the 'Subscribe' checkbox");await stagehand.act("select 'Option 2' from the dropdown");await stagehand.act("upload a file to the file input");
await stagehand.act("scroll to the bottom of the page");await stagehand.act("scroll to the 'Contact Us' section");await stagehand.act("click the 'Next Page' button");