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.
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.
Suggested actions
Suggested actions
| Action | Example instruction |
|---|---|
| Click | click the button |
| Fill | fill the field with <value> |
| Type | type <text> into the search box |
| Press | press <key> in the search field |
| Scroll | scroll to <position> |
| Select from dropdown | select <value> from the dropdown |
Return value of act()?
When you use act(), Stagehand will return a Promise<ActResult> with the following structure:
- Do this
- Don't do this
Break your task into single-step actions.
Advanced Configuration
You can pass additional options to configure the model, timeout, variables, and target page:Using with Custom Pages
You can useact() with pages from other browser automation libraries like Puppeteer, Playwright, or Patchright by passing the page option:
- Puppeteer: Pass Puppeteer Page objects
- Playwright: Pass Playwright Page objects
- Patchright: Pass Patchright Page objects
- Stagehand Page: Use
stagehand.context.pages()[0]orcontext.activePage()(default)
Complete API Reference
See the full
act() 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). 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 acacheDir 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
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 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
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: Learn more about working with iframes
- Increase action timeout
- Use
observe()first to verify element exists
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
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.
