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.
Common use cases
Common use cases
| Use Case | Example instruction |
|---|---|
| Find buttons | find the submit button |
| Locate forms | find all input fields in the form |
| Discover links | find navigation links |
| Identify tables | find the pricing table |
| Map workflows | find all checkout steps |
| Validate elements | find the delete account button |
Return value of observe()?
When you use observe(), Stagehand will return a Promise<Action[]> with the following structure:
- Do this
- Don't do this
Use specific, descriptive instructions.
Advanced Configuration
You can pass additional options to configure the model, timeout, and selector scope:Using with Custom Pages
You can useobserve() 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 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 separateact() calls.
Analyze pages with observe()
Complete guide to planning actions with
observe().Scope extractions
Useobserve() 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
No elements found
No elements found
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
Inaccurate results
Inaccurate results
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
logInferenceToFilein Stagehand configuration to inspect LLM reasoning
Wrong method suggested
Wrong method suggested
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.
