What is act()?

page.act("click on add to cart")
act enables Stagehand to perform individual actions on a web page. Use it to build self-healing and deterministic automations that adapt to website changes.

Why use act()?

Using act()

Use act to perform single actions in your automation. Here’s how to click a button:
await page.goto("https://example-store.com");
await page.act("click the add to cart button");
With act, breaking complex actions into small, single-step actions works best. If you need to orchestrate multi-step flows, use multiple act commands or agent.
Break your task into single-step actions.
// Break it into single-step actions
await page.act("open the filters panel");
await page.act("choose 4-star rating");
await page.act("click the apply button");

Advanced Configuration

For advanced scenarios, you can configure additional options:
// Dynamic food search with advanced options
const foodItem = "organic quinoa";

await page.act({
  action: "Type %foodItem% in the search box and press enter",
  variables: {
    foodItem: foodItem
  },
  modelName: "google/gemini-2.5-pro",
  modelClientOptions: {
    modelApiKey: process.env.GOOGLE_API_KEY,
  },
  iframes: true, // Search within iframes if needed
  domSettleTimeoutMs: 45000, // Wait longer for dynamic content
  timeoutMs: 60000 // Extended timeout for slow-loading forms
});
Shadow DOM support is now available! Set experimental: true in your Stagehand configuration to enable it. See the configuration guide for more details.

Best practices

Ensure reliable actions

Use observe() to discover candidate actions on the current page and plan reliably. It returns a list of suggested actions (with selector, description, method, and arguments). You can pass an observed action directly to act to execute it.
const [action] = await page.observe("click the login button");

if (action) {
  await page.act(action);
}

Analyze pages with observe()

Plan actions with observe() before executing with act.

Reduce model costs

Cache observed actions to avoid repeated LLM calls and ensure consistent execution.
// Cost-optimized actions with caching
const actionCache = new Map();

const getCachedAction = async (instruction: string) => {
  if (actionCache.has(instruction)) {
    return actionCache.get(instruction);
  }
  
  const [action] = await page.observe(instruction);
  actionCache.set(instruction, action);
  return action;
};

// Reuse cached actions
const loginAction = await getCachedAction("click the login button");
await page.act(loginAction);

Complete caching guide

Learn advanced caching techniques and patterns for optimal performance.

Secure your automations

Variables will not be shared with LLM providers. Use them for passwords, API keys, and other sensitive data.
Load sensitive data from environment variables using .env files. Never hardcode API keys, passwords, or other secrets directly in your code.
await page.act({
  action: "enter %username% in the email field",
  variables: {
    username: "user@example.com"
  }
});

await page.act({
  action: "enter %password% in the password field",
  variables: {
    password: process.env.USER_PASSWORD
  }
});
When handling sensitive data, set verbose: 0 in your Stagehand configuration to prevent secrets from appearing in logs. See the configuration guide for more details.

User Data Best Practices

Complete guide to securing your browser automations with best practices and configurations.

Troubleshooting

Next steps