Skip to main content

Overview

Stagehand v3 can work seamlessly with Playwright, allowing you to use Playwright’s Page objects directly with Stagehand’s AI-powered methods like act(), extract(), and observe().

Installation

First, install both Stagehand and Playwright:

Quickstart

Basic Setup

Connect Playwright to Stagehand’s browser instance using Chrome DevTools Protocol (CDP):

Using Playwright Pages with Stagehand

Once connected, you can use Playwright’s Page objects with Stagehand’s AI-powered methods:

Multi-Page Example

Stagehand works great with multiple Playwright pages:

Complete Example

Here’s a full working example:

Key Points

  • Connect via CDP: Use chromium.connectOverCDP() with stagehand.connectURL() as the WebSocket endpoint
  • Pass the page: Always pass the Playwright page object to Stagehand methods using the { page } option
  • Multi-page support: Create multiple pages with pwContext.newPage() and pass them to Stagehand methods
  • Concurrent operations: Use Promise.all() to run multiple Stagehand operations in parallel across different pages

Environment Variables

When using Browserbase, set your credentials:
For OpenAI (or other providers):

Next Steps

Agent

Automate entire workflows

Act

Execute actions on web pages

Extract

Extract structured data from pages

Observe

Observe and find elements on pages