Documentation Index
Fetch the complete documentation index at: https://docs.driver.dev/llms.txt
Use this file to discover all available pages before exploring further.
Stagehand is an AI-native browser automation framework that uses natural language to interact with web pages.
Installation
npm install @browserbasehq/stagehand @browsercash/sdk
Quick Start
import { Stagehand } from '@browserbasehq/stagehand';
import BrowsercashSDK from '@browsercash/sdk';
async function run() {
const client = new BrowsercashSDK({
apiKey: process.env.DRIVER_API_KEY!,
});
// Create Driver session
const session = await client.browser.session.create();
// Initialize Stagehand with Driver CDP URL
const stagehand = new Stagehand({
env: 'LOCAL',
localBrowserLaunchOptions: {
cdpUrl: session.cdpUrl,
},
});
await stagehand.init();
// Use natural language to interact with pages
await stagehand.page.goto('https://example.com');
await stagehand.act({ action: 'click on the login button' });
await stagehand.act({ action: 'type "[email protected]" in the email field' });
await stagehand.act({ action: 'type "password123" in the password field' });
await stagehand.act({ action: 'click submit' });
// Extract data using natural language
const data = await stagehand.extract({
instruction: 'Extract the user profile information',
schema: {
type: 'object',
properties: {
name: { type: 'string' },
email: { type: 'string' },
},
},
});
console.log(data);
// Cleanup
await stagehand.close();
await client.browser.session.stop({ sessionId: session.sessionId });
}
run().catch(console.error);
Key Features
Natural Language Actions
// Click elements
await stagehand.act({ action: 'click the sign up button' });
// Fill forms
await stagehand.act({ action: 'enter "John Doe" in the name field' });
// Navigate
await stagehand.act({ action: 'scroll down to the pricing section' });
const products = await stagehand.extract({
instruction: 'Extract all products with their prices',
schema: {
type: 'array',
items: {
type: 'object',
properties: {
name: { type: 'string' },
price: { type: 'number' },
inStock: { type: 'boolean' },
},
},
},
});
Observe Page State
const observations = await stagehand.observe({
instruction: 'What actions can I take on this page?',
});
With Session Options
const session = await client.browser.session.create({
country: 'US',
type: 'consumer_distributed',
windowSize: '1920x1080',
profile: { name: 'stagehand-session', persist: true },
});
Complex Workflows
// Multi-step workflow with error handling
async function checkoutFlow(stagehand: Stagehand) {
await stagehand.page.goto('https://shop.example.com');
await stagehand.act({ action: 'search for "laptop"' });
await stagehand.act({ action: 'click on the first product' });
await stagehand.act({ action: 'add to cart' });
await stagehand.act({ action: 'go to checkout' });
const cartSummary = await stagehand.extract({
instruction: 'Extract the cart total and item count',
schema: {
type: 'object',
properties: {
total: { type: 'string' },
itemCount: { type: 'number' },
},
},
});
return cartSummary;
}
Why Stagehand + Driver?
- AI-native — Use natural language instead of brittle selectors
- Self-healing — Adapts to UI changes automatically
- Stealth — Driver’s consumer nodes avoid detection
- Scalable — No local browser management needed