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 will return a Promise<Action[]> with the following structure:
Use specific, descriptive instructions.

Advanced Configuration

You can pass additional options to configure the model, timeout, selector scope, ignored page regions, and placeholder variables:
You can also exclude specific nodes, including their descendant nodes, with ignoreSelectors.
ignoreSelectors removes all matches for each selector, along with each matched node’s descendants. selector still scopes observation to a single resolved subtree.

Validate Then Act with Variables

For login and other safety-sensitive flows, use observe() to discover candidate actions, validate them, and then execute them with act(). When you pass variables, observe() returns %variableName% placeholders in the suggested action arguments instead of raw secret values.

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 observe() 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.
observe() does not currently expose a cacheStatus field. To check whether an observe() call was served from cache, use the Browserbase session replay dashboard or inspect the session logs.

Using with Custom Pages

You can use observe() 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 Context Pages: Access pages via stagehand.context.pages() (default)

Complete API Reference

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

Best practices

Plan then execute

Discover all actions once, then execute without additional LLM calls. This approach is 2-3x faster than separate act() calls.

Analyze pages with observe()

Complete guide to planning actions with observe().

Scope extractions

Use observe() to narrow extraction scope and reduce token usage by up to 10x.

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

Complete caching guide

Learn advanced caching techniques and patterns for optimal performance.

Troubleshooting

Problem: observe() returns empty arraySolutions:
  • 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()
  • Enable verbose logging in Stagehand configuration to inspect detection behavior
Problem: Descriptions or selectors don’t match actual elementsSolutions:
  • Use more capable models—check model evals for recommendations
  • Provide more context in your instruction (e.g., “find the submit button in the checkout form”)
  • Enable verbose logging and logInferenceToFile in Stagehand configuration to inspect LLM reasoning
Problem: The method field has an unexpected valueSolutions:
  • Validate the method before using it: if (action.method === "click") { ... }
  • Check supported actions for valid method names
  • Override with a specific method when needed: await stagehand.act({ ...action, method: "click" })

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 observe() reference with detailed parameter documentation.