What is act()?
- TypeScript
- Python
- Go
act performs one action on a web page. Chain single-step calls to build automations that survive website changes.
act accepts either a natural language instruction or an Action returned by observe(). Passing an instruction runs model inference to find the target; passing an Action replays it deterministically with no inference at all.
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
Turn on
selfHeal to re-infer an action when its recorded selector breaks.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:
- TypeScript
- Python
- Go
iFrame and Shadow DOM support Stagehand automatically handles iFrame traversal and shadow DOM elements without requiring additional configuration. Page snapshots merge every frame’s accessibility tree by default.
act, breaking complex actions into small, single-step actions works best. If you need to orchestrate multi-step flows, use multiple act commands.
Suggested actions
Suggested actions
The
method column is the value you will see on an Action returned by observe().Return value of act()
When you use act(), Stagehand returns a result with two fields: data holds the action payload, and metadata carries the action ID and server-side cache status.
- TypeScript
- Python
- Go
- Do this
- Don't do this
Break your task into single-step actions.
- TypeScript
- Python
- Go
Advanced configuration
You can pass additional options to configure the model, timeout, variables, target page, and target locator:- TypeScript
- Python
- Go
act() calls. When you pass an Action returned by observe(), Stagehand replays that action’s selector directly.
Server-side caching
cache requires a Browserbase browser and a Browserbase API key. It has no effect on local browsers.act() results server-side. Repeated calls with the same inputs return instantly without consuming LLM tokens. Enable caching on the constructor and override it per call:
Instruction-based act() calls with a target locator or ignored locators bypass the server-side cache and report metadata.cache.status as DISABLED.
- TypeScript
- Python
- Go
Complete caching guide
Learn how the cache key is built, what the threshold does, and when results are invalidated.
Using with custom pages
Stagehand v4 drives the browser directly over the Chrome DevTools Protocol, so there is no Puppeteer, Playwright, or Patchright page interop. Instead, target any page Stagehand manages by passing thepage option:
- TypeScript
- Python
- Go
- Active page: omit
pageand Stagehand usescontext.activePage()(default) - Existing pages: receive the list from
context.pages()first (await it in TypeScript and Python), then index into it - New pages: create one with
context.newPage() - Existing browsers: attach to a browser you already run with
localBrowser.connect()
Complete API reference
See the full
Stagehand reference for detailed parameter documentation, return values, and advanced examples.Best practices
Ensure reliable actions
Useobserve() to discover candidate actions on the current page and plan reliably. It returns a list of suggested actions (with selector, description, method, and arguments). Inspect the action before you commit to it, then hand it straight back to act:
- TypeScript
- Python
- Go
Replaying an
Action skips model inference, the page snapshot, and the DOM-settle wait, and it does not consult the server-side cache. If the recorded selector no longer resolves and selfHeal is on, Stagehand re-infers the action and retries once.Analyze pages with observe()
Plan actions with
observe() before executing with act.Reduce model costs
Enable server-side caching withcache when running on Browserbase. Stagehand records the action on the first run; once the hit count meets the threshold, the cache serves identical calls without an LLM call.
- TypeScript
- Python
- Go
The cache lives on Browserbase, keyed on the instruction, page content, and call options, so it persists across script executions and across machines. The model configuration is deliberately excluded from the key, so switching models does not invalidate your cache.
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. Stagehand exposes only the variable names to the model and substitutes the real values locally, so results record the placeholder rather than the secret. One exception: with server-side caching enabled, variable values travel to the cache service as part of the request, so turn thecache option off for calls that carry credentials.
Load sensitive data from environment variables. Never hardcode API keys, passwords, or other secrets directly in your code.
- TypeScript
- Python
- Go
User data best practices
Complete guide to persisting and securing browser state across sessions.
Troubleshooting
Method not supported
Method not supported
Problem: Solution 2: Retry with exponential backoff
act fails with “method not supported” errorSolutions:- Use clear and detailed instructions for what you want to accomplish
- Review the Stagehand evals to find the best models for your use case
- Use
observe()and verify the resulting action is within a list of expected actions
- TypeScript
- Python
- Go
- TypeScript
- Python
- Go
Action failed or timed out
Action failed or timed out
Problem:
act times out or fails to complete action (often due to element not found)Solutions:- Ensure page has fully loaded
- Check if content is in iframes: Stagehand traverses them automatically, but a target locator can help
- Increase action timeout
- Use
observe()first to verify element exists - Raise
domSettleTimeoutMson the constructor if the page keeps mutating after load
- TypeScript
- Python
- Go
Incorrect element selected
Incorrect element selected
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
- TypeScript
- Python
- Go
Next steps
Discover actions with observe()
Use
observe() to plan actions before executing them.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.Work across multiple tabs
Target a specific page with the
page option.
