Skip to main content

What is act()?

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()?

Natural Language Instructions

Write automation in plain English. No selectors or complex syntax.

Precise Control

Build automations step by step. Define exactly what happens at every moment.

Self-Healing

Actions automatically adapt when websites change.

Caching

Cache actions to avoid LLM calls and ensure consistent execution across runs.

Using act()

Use act to perform single actions in your automation. Here’s how to click a button:
iFrame and Shadow DOM Support Stagehand automatically handles iFrame traversal and shadow DOM elements without requiring additional configuration.
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.

Return value of act()?

When you use act(), Stagehand will return a Promise<ActResult> with the following structure:
Break your task into single-step actions.

Advanced Configuration

You can pass additional options to configure the model, timeout, variables, and target page:

Server-side Caching

serverCache only works when running with env: "BROWSERBASE". It has no effect in local environments.
When running on Browserbase, Stagehand automatically caches act() results server-side. Repeated calls with the same inputs return instantly without consuming LLM tokens. Caching is enabled by default and can be controlled globally on the constructor or overridden per call:
For how the cache works, tuning the validation threshold, and best practices, see the Server Caching guide.

Using with Custom Pages

You can use act() with pages from other browser automation libraries like Puppeteer, Playwright, or Patchright by passing the page option:
This works with:
  • Puppeteer: Pass Puppeteer Page objects
  • Playwright: Pass Playwright Page objects
  • Patchright: Pass Patchright Page objects
  • Stagehand Page: Use stagehand.context.pages()[0] or context.activePage() (default)

Complete API Reference

See the full act() reference for detailed parameter documentation, return values, and advanced examples.

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.

Analyze pages with observe()

Plan actions with observe() before executing with act.

Reduce model costs

Enable automatic action caching by specifying a cacheDir when initializing Stagehand. The first time an action runs, it’s cached. Subsequent runs reuse the cached action without LLM calls.
Caching persists across script executions. The first time you run your script, actions are cached to your local filesystem. On subsequent runs, cached actions are reused automatically, significantly reducing costs and improving performance.

Complete caching guide

Learn advanced caching techniques and patterns for optimal performance.

Secure your automations

Variables are not 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.
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

Problem: act fails with “method not supported” errorSolutions:
  • Use clear and detailed instructions for what you want to accomplish
  • Review our evals to find the best models for your use case
  • Use observe() and verify the resulting action is within a list of expected actions
Solution 1: Validate with observe
Solution 2: Retry with exponential backoff
Problem: act times out or fails to complete action (often due to element not found)Solutions:
Problem: act performs action on wrong elementSolutions:
  • Be more specific in instructions: include visual cues, position, or context
  • Use observe() to preview which element will be selected
  • Add contextual information: “the search button in the header”
  • Use unique identifiers when available

Next steps

Orchestrate complex workflows with Agent

Use Agent to autonomously execute multi-step tasks and complex workflows.

Caching actions

Speed up repeated automations by caching actions.

Extract data with extract()

Use extract with a data schema to pull clean, typed data from any page.

Preview actions with observe()

Preview actions with observe() before executing them.