What is observe()?
- TypeScript
- Python
- Go
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:
- TypeScript
- Python
- Go
iFrame and Shadow DOM support Stagehand automatically handles iFrame traversal and shadow DOM elements without requiring additional configuration.
Common use cases
Common use cases
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:
- TypeScript
- Python
- Go
- Do this
- Don't do this
Use specific, descriptive instructions.
- TypeScript
- Python
- Go
Advanced configuration
You can pass additional options to configure the model, timeout, locator scope, ignored page regions, and placeholder variables:- TypeScript
- Python
- Go
- TypeScript
- Python
- Go
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, useobserve() 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.
- TypeScript
- Python
- Go
Server-side caching
cache requires a Browserbase browser and a Browserbase API key. It has no effect on local browsers.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.
- TypeScript
- Python
- Go
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 thepage option:
- TypeScript
- Python
- Go
- Active page: omit
pageand 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 toact(). 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.
- TypeScript
- Python
- Go
Analyze pages with observe()
Complete guide to planning actions with
observe().Scope extractions
Useobserve() 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.
- TypeScript
- Python
- Go
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.- TypeScript
- Python
- Go
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:- TypeScript
- Python
- Go
Complete caching guide
Learn advanced caching techniques and patterns for optimal performance.
Troubleshooting
No elements found
No elements found
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
debugin your Stagehand configuration to inspect detection behavior
- TypeScript
- Python
- Go
Inaccurate results
Inaccurate results
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
debugin your Stagehand configuration to inspect LLM reasoning
- TypeScript
- Python
- Go
Wrong method suggested
Wrong method suggested
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
- TypeScript
- Python
- Go
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.
