Stagehand — AI-Powered Browser Automation SDK
TypeScript SDK that lets you automate browsers using natural language and visual understanding. AI sees the page like a human does. Built on Playwright. 10,000+ GitHub stars.
What it is
Stagehand is a TypeScript SDK that lets you automate browsers using natural language and visual understanding. Instead of writing CSS selectors or XPath queries, you describe what you want in plain English and Stagehand's AI figures out how to interact with the page. It is built on Playwright, so you get the reliability of a mature browser automation framework with AI-powered element selection.
Stagehand is designed for developers building web scrapers, test automation, or browser-based workflows who want to reduce the fragility of selector-based automation.
How it saves time or tokens
Traditional browser automation breaks when websites change their HTML structure, CSS classes, or DOM hierarchy. Stagehand's AI-based element selection is resilient to these changes because it understands the visual layout and semantic meaning of page elements. A test that says 'click the login button' works regardless of whether the button's class name changes. This reduces maintenance time and the cost of keeping automation scripts up to date.
How to use
- Install Stagehand:
npm install @browserbasehq/stagehand
- Create an automation script:
import { Stagehand } from '@browserbasehq/stagehand';
const stagehand = new Stagehand({ env: 'LOCAL' });
await stagehand.init();
await stagehand.page.goto('https://news.ycombinator.com');
// AI-powered actions
await stagehand.act({ action: 'click the first article link' });
const title = await stagehand.extract({
instruction: 'extract the article title',
schema: { type: 'object', properties: { title: { type: 'string' } } },
});
console.log(title);
- Run your script with Node.js or integrate it into your test suite.
Example
A web scraping workflow that extracts product data:
import { Stagehand } from '@browserbasehq/stagehand';
const stagehand = new Stagehand({ env: 'LOCAL' });
await stagehand.init();
await stagehand.page.goto('https://example-store.com/products');
const products = await stagehand.extract({
instruction: 'extract all product names, prices, and ratings from the page',
schema: {
type: 'array',
items: {
type: 'object',
properties: {
name: { type: 'string' },
price: { type: 'string' },
rating: { type: 'number' },
},
},
},
});
console.log(JSON.stringify(products, null, 2));
await stagehand.close();
Related on TokRepo
- Browser automation tools — Browse AI-powered browser tools
- Web scraping tools — Explore web data extraction tools
Common pitfalls
- Using Stagehand for tasks where simple selectors work fine. AI-based selection adds latency and API costs. Use traditional Playwright selectors for stable, well-known page structures and Stagehand for dynamic or frequently changing pages.
- Not handling authentication cookies. For pages behind login, handle authentication in a traditional Playwright step before using Stagehand's AI features.
- Expecting 100% accuracy on complex pages. AI-based element selection is probabilistic. Add assertions and error handling to catch cases where the AI selects the wrong element.
Frequently Asked Questions
Stagehand uses an AI model that takes a screenshot and the page's accessibility tree as input. It identifies elements by their visual appearance and semantic meaning rather than CSS selectors. This makes it resilient to DOM structure changes.
Stagehand works with any website that Playwright can render. It supports modern JavaScript-heavy single-page applications, server-rendered pages, and static sites. Some sites with aggressive bot detection may require additional configuration.
Yes. Stagehand extends Playwright, so you have full access to Playwright's API alongside Stagehand's AI actions. Use Playwright for navigation and known interactions, and Stagehand's act/extract for dynamic elements.
Stagehand supports multiple AI providers. By default, it uses the model configured in your environment. You can specify the model provider and API key in the Stagehand constructor options.
Stagehand can be used in production, but consider the AI API costs per action. For high-volume scraping, use Stagehand to generate selectors once, then switch to pure Playwright for production runs to reduce costs.
Citations (3)
- Stagehand GitHub— Stagehand is a TypeScript SDK for AI browser automation
- Playwright Documentation— Built on Playwright browser automation framework
- Browserbase— Browserbase cloud browser infrastructure
Related on TokRepo
Source & Thanks
Created by Browserbase. Licensed under MIT.
stagehand — stars 10,000+
Thanks for making browser automation speak human.
Discussion
Related Assets
Claude-Flow — Multi-Agent Orchestration for Claude Code
Layers swarm and hive-mind multi-agent orchestration on top of Claude Code with 64 specialized agents, SQLite memory, and parallel execution.
ccusage — Real-Time Token Cost Tracker for Claude Code
CLI that reads ~/.claude logs and breaks down Claude Code token spend by day, session, and project — pluggable into your statusline.
SuperClaude — Workflow Framework for Claude Code
Adds 16+ slash commands, 9 cognitive personas, and a smart flag system to Claude Code in one pipx install.