Skip to main content

What is observe()?

observe() discovers actionable elements on a page and returns structured actions you can execute or validate before acting. Use it to explore pages, plan multi-step workflows, cache actions, and validate elements before acting.

Why use observe()?

Explore

Discover what’s possible on a page: find buttons, forms, links, and interactive elements

Plan

Map out multi-step workflows by discovering all required actions upfront

Cache

Store discovered actions to skip LLM calls and speed up repeated workflows

Validate

Verify elements exist and check their properties before performing critical actions

Using observe()

Use observe() to discover actionable elements on a page. Here’s how to find a button:
iFrame and Shadow DOM support Stagehand automatically handles iFrame traversal and shadow DOM elements without requiring additional configuration.

Return value of observe()

When you use observe(), Stagehand returns a result whose data field is a list of Action objects, alongside metadata carrying the action ID and server-side cache status. Each Action can be passed straight to act() for deterministic replay:
Use specific, descriptive instructions.

Advanced configuration

You can pass additional options to configure the model, timeout, locator scope, ignored page regions, and placeholder variables:
You can also exclude specific nodes, including their descendant nodes, with ignored locators.
ignoreLocators remove each resolved locator target and its descendants from the snapshot. A locator without nth removes all matching targets; a locator with nth removes only that indexed match. The locator option scopes observation to one resolved subtree, using nth when present. Scoped observe currently supports CSS and XPath locators. text= locators are supported by locator methods, but not yet by observe snapshot scoping.

Validate then act with variables

For login and other safety-sensitive flows, use observe() to discover candidate actions, validate them, and then execute them. When you pass variables, observe() returns %variableName% placeholders in the suggested action arguments instead of raw secret values, so no secret ever reaches the model.
act() supports the same variables option with either input form. Pass an instruction when you want Stagehand to locate the field for you, or pass the observed Action to replay a known target, in both cases keeping the value out of the prompt.

Server-side caching

cache requires a Browserbase browser and a Browserbase API key. It has no effect on local browsers.
When running on Browserbase, Stagehand can cache observe() 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: Locator-scoped observations, including calls with locator or ignoreLocators, bypass the server-side cache and report metadata.cache.status as DISABLED.
Cache status is reported on the result metadata, alongside the observed actions. You can also see it in the Browserbase session replay dashboard or in the session logs at debug level.

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 the page option:
This works with:
  • Active page: omit page and Stagehand uses the active page (default)
  • Existing pages: index into the list of context pages
  • New pages: create one on the context
  • 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

Plan then execute

Discover all actions once, then feed each one back to act(). Passing an action rather than a string skips inference, so the loop below makes one model call for the whole form instead of one per field.

Analyze pages with observe()

Complete guide to planning actions with observe().

Scope extractions

Use observe() to find a container, then create a page locator from the observed selector and pass that locator to extract(). Extraction then sees only that subtree instead of the whole page, so token usage falls in proportion to how much of the page you exclude.

Extract structured data

Learn how to use observe() with extract() for precise data extraction.

Validate before acting

Check elements exist and verify their properties before performing critical operations.

Execute actions with act()

Learn how to execute observed actions reliably.

Cache observed actions

Store and reuse observed actions to eliminate redundant LLM calls. Build a simple in-process cache:

Complete caching guide

Learn advanced caching techniques and patterns for optimal performance.

Troubleshooting

Problem: observe() returns an empty listSolutions:
  • Verify the element exists on the page
  • Use more specific instructions (e.g., “find the blue submit button” instead of “find button”)
  • Ensure page has fully loaded before calling observe()
  • Set the log level to debug in your Stagehand configuration to inspect detection behavior
Problem: Descriptions or selectors don’t match actual elementsSolutions:
  • Use more capable models, and check model evals for recommendations
  • Provide more context in your instruction (e.g., “find the submit button in the checkout form”)
  • Set the log level to debug in your Stagehand configuration to inspect LLM reasoning
Problem: The method field has an unexpected valueSolutions:
  • Validate the method before using it
  • Check supported actions for valid method names
  • Reach for a locator when you want to call a specific method instead of trusting the suggestion

Next steps

Execute actions with act()

Use act() to execute discovered actions reliably.

Extract structured data

Combine observe() with extract() for precise data extraction.

Caching actions

Build action caches to eliminate redundant LLM calls.

Complete API reference

Full Stagehand reference with detailed parameter documentation.