stagehand
You are an expert in Stagehand by BrowserBase, the AI-powered browser automation framework that lets you control web pages using natural language instructions. You help developers build web automations that act, extract data, and observe pages using plain English commands instead of brittle CSS selectors — powered by GPT-4o or Claude for visual understanding of page layouts.
What this skill does
# Stagehand — AI Browser Automation in Natural Language
You are an expert in Stagehand by BrowserBase, the AI-powered browser automation framework that lets you control web pages using natural language instructions. You help developers build web automations that act, extract data, and observe pages using plain English commands instead of brittle CSS selectors — powered by GPT-4o or Claude for visual understanding of page layouts.
## Core Capabilities
### Setup and Basic Actions
```typescript
import { Stagehand } from "@browserbasehq/stagehand";
const stagehand = new Stagehand({
env: "LOCAL", // "LOCAL" for Playwright, "BROWSERBASE" for cloud
modelName: "gpt-4o",
modelClientOptions: { apiKey: process.env.OPENAI_API_KEY },
enableCaching: true, // Cache AI decisions for repeated patterns
});
await stagehand.init();
const page = stagehand.page; // Standard Playwright page object
// Navigate
await page.goto("https://app.example.com");
// Act — natural language browser control
await stagehand.act({ action: "Click the sign-in button" });
await stagehand.act({ action: "Type '[email protected]' into the email field" });
await stagehand.act({ action: "Select 'Enterprise' from the plan dropdown" });
await stagehand.act({ action: "Scroll down to the pricing section" });
// Act with variables — keep credentials out of prompts
await stagehand.act({
action: "Log in with username %user% and password %pass%",
variables: {
user: process.env.USERNAME!,
pass: process.env.PASSWORD!,
},
});
```
### Extract Structured Data
```typescript
// Extract structured data from any page
const products = await stagehand.extract({
instruction: "Extract all product listings with name, price, rating, and availability",
schema: {
type: "object",
properties: {
products: {
type: "array",
items: {
type: "object",
properties: {
name: { type: "string" },
price: { type: "number" },
rating: { type: "number" },
inStock: { type: "boolean" },
},
required: ["name", "price"],
},
},
},
},
});
// Extract from complex pages (tables, nested layouts)
const invoiceData = await stagehand.extract({
instruction: "Extract the invoice number, date, line items with quantities and amounts, and the total",
schema: invoiceSchema,
});
```
### Observe — Find Elements Without Acting
```typescript
// Observe returns possible actions without performing them
const actions = await stagehand.observe({
instruction: "Find all clickable navigation items",
});
// Returns: [{description: "Home link", selector: "xpath=...", ...}, ...]
// Use observe for conditional logic
const buttons = await stagehand.observe({
instruction: "Find the 'Accept cookies' button if it exists",
});
if (buttons.length > 0) {
await stagehand.act({ action: "Dismiss the cookie popup" });
}
```
### Cloud Execution with BrowserBase
```typescript
// Run in cloud for parallel, scalable automation
const stagehand = new Stagehand({
env: "BROWSERBASE", // Cloud-hosted browser
modelName: "gpt-4o",
browserbaseSessionCreateParams: {
projectId: process.env.BROWSERBASE_PROJECT_ID!,
proxies: true, // Residential proxy
},
});
```
## Installation
```bash
npm install @browserbasehq/stagehand
# Requires: OPENAI_API_KEY or ANTHROPIC_API_KEY
# Optional: BROWSERBASE_API_KEY + BROWSERBASE_PROJECT_ID for cloud
```
## Best Practices
1. **Natural language for dynamic pages** — Use `act()` for pages that change layout frequently; CSS selectors break, natural language adapts
2. **Variables for secrets** — Never put credentials in action strings; use the `variables` parameter
3. **Enable caching** — Set `enableCaching: true` to avoid repeated AI calls for identical actions; huge cost savings
4. **Combine with Playwright** — Use `stagehand.page` for stable interactions (login forms) and `stagehand.act()` for dynamic ones
5. **Schema for extraction** — Always provide a Zod/JSON schema to `extract()`; structured output is more reliable than free-text
6. **Observe before acting** — Use `observe()` to check if elements exist before acting; prevents errors on conditional UI
7. **BrowserBase for scale** — Use cloud browsers for parallel automation; local is fine for development and testing
8. **Model selection** — GPT-4o for speed, Claude for complex visual reasoning; both work well for most tasks
Related in Web Dev
generating-lwc-components
IncludedLightning Web Components with PICKLES methodology and 165-point scoring. Use this skill when the user creates or edits LWC components, builds wire service patterns, or writes Jest tests for LWC. TRIGGER when: user creates/edits LWC components, touches lwc/**/*.js, .html, .css, .js-meta.xml files, or asks about wire service, SLDS, or Jest LWC tests. DO NOT TRIGGER when: Apex classes (use generating-apex), Aura components, or Visualforce.
tanstack-query
IncludedManage server state in React with TanStack Query v5. Set up queries with useQuery, mutations with useMutation, configure QueryClient caching strategies, implement optimistic updates, and handle infinite scroll with useInfiniteQuery. Use when: setting up data fetching in React projects, migrating from v4 to v5, or fixing object syntax required errors, query callbacks removed issues, cacheTime renamed to gcTime, isPending vs isLoading confusion, keepPreviousData removed problems.
document-processor-api
IncludedProcess documents with Nutrient DWS. Use when the user wants to generate PDFs from HTML or URLs, convert Office/images/PDFs, assemble or split packets, OCR scans, extract text/tables/key-value pairs, redact PII, watermark, sign, fill forms, optimize PDFs, or produce compliance outputs like PDF/A or PDF/UA. Triggers include convert to PDF, merge these PDFs, OCR this scan, extract tables, redact PII, sign this PDF, make this PDF/A, or linearize for web delivery.
nutrient-document-processing
IncludedProcess documents with Nutrient DWS. Use when the user wants to generate PDFs from HTML or URLs, convert Office/images/PDFs, assemble or split packets, OCR scans, extract text/tables/key-value pairs, redact PII, watermark, sign, fill forms, optimize PDFs, or produce compliance outputs like PDF/A or PDF/UA. Triggers include convert to PDF, merge these PDFs, OCR this scan, extract tables, redact PII, sign this PDF, make this PDF/A, or linearize for web delivery.
tanstack-query
IncludedManage server state in React with TanStack Query v5. Covers useMutationState, simplified optimistic updates, throwOnError, network mode (offline/PWA), and infiniteQueryOptions. Use when setting up data fetching, fixing v4→v5 migration errors (object syntax, gcTime, isPending, keepPreviousData), or debugging SSR/hydration issues with streaming server components.
accelint-nextjs-best-practices
IncludedNext.js performance optimization and best practices. Use when writing Next.js code (App Router or Pages Router); implementing Server Components, Server Actions, or API routes; optimizing RSC serialization, data fetching, or server-side rendering; reviewing Next.js code for performance issues; fixing authentication in Server Actions; or implementing Suspense boundaries, parallel data fetching, or request deduplication.