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:
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.

Advanced Configuration

For advanced scenarios, you can configure additional options:
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.

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.

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.
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.

Working with iframes

Learn best practices for interacting with elements inside iframes.