Guide

Convert to Stagehand Script

Overview

Browserbase provides a cloud-based playground where you can write, convert, and test browser automation scripts. Stagehand is a newer framework for building browser agents that work with Browserbase, offering a more intuitive, prompt-driven approach to web automation compared to traditional Playwright scripts. This workflow guides you through converting an existing Playwright script to Stagehand format and running it immediately to verify the conversion works correctly. This is useful when you want to migrate legacy Playwright automation to Stagehand's agent-based capabilities, or when you prefer the cleaner syntax and AI-powered interaction model that Stagehand provides.

Before you begin

  • Access to www.browserbase.com with an active Browserbase account (sign up at https://www.browserbase.com/sign-up if needed)
  • A Playwright script ready to convert, copied to your clipboard
  • Understanding of basic browser automation concepts (Playwright or similar)
  • Basic familiarity with code editors and JavaScript/TypeScript syntax

Step by step

1
ClickPlayground

Navigate to the Browserbase Playground by clicking the 'Playground' link in the main navigation. This is where you can write, test, and convert browser automation scripts in a cloud-hosted environment.

Tip. The Playground provides a pre-configured environment with Playwright and Browserbase libraries already loaded, so you do not need to set up dependencies locally.
Step 1
2
Clickconsole.info("Launching browser..."); /* To make things easier, we've setup Playwright using the window variables. You can access it and your API key using window.playwright or window.connectionString. */ console.info('Connected!'); /** * 👋 Welcome

Click inside the code editor area to focus it. The editor contains a welcome template and placeholder code. You will replace this with your Playwright script.

Tip. The editor area is marked with the text 'Editor content' and has keyboard shortcuts available. Use Alt+F1 if you need accessibility options.
Step 2
3
Clickconsole.info("Launching browser..."); /* To make things easier, we've setup Playwright using the window variables. You can access it and your API key using window.playwright or window.connectionString. */ console.info('Connected!'); /** * 👋 Welcome

Click again to ensure the editor is fully focused and ready to receive input. This ensures that your subsequent paste operation will insert the script into the correct location.

Step 3
4
Keyboard

Paste your Playwright script from the clipboard by pressing Cmd+V (or Ctrl+V on Windows/Linux). This inserts the script into the editor, replacing any template code.

Tip. Ensure your Playwright script is already copied to your clipboard before this step. If paste does not work, try clicking in the editor again and verify your clipboard contains valid code.
Step 4
5
TypeEditor content;Press Alt+F1 for Accessibility Options.

If needed, manually add or adjust any script content in the editor textbox. This step allows you to make quick edits to the Playwright code before conversion, such as fixing syntax errors or removing incompatible features.

Tip. Use standard editor shortcuts (Cmd+Z to undo, Cmd+A to select all) to manage your script. Keep your script focused and well-formed for better conversion results.
6
ClickConvert to Stagehand

Click the 'Convert to Stagehand' button to automatically transform your Playwright script into a Stagehand-compatible script. Browserbase will analyze your code and rewrite it using Stagehand's agent-based APIs.

Tip. The conversion is designed to preserve the intent of your automation logic. Review the converted code carefully, as some Playwright patterns may require manual adjustment for Stagehand's capabilities.
Warning. The conversion tool works best with straightforward Playwright scripts. Complex scripts with custom logic or unsupported APIs may require manual refinement after conversion.
Step 6
7
ClickAccept changes

Review the converted script and click 'Accept changes' to confirm the conversion. This step commits the Stagehand script as your active code in the playground.

Tip. Take a moment to scan the converted code for any obvious errors or missing logic. If something looks wrong, you can reject the changes and manually edit the original script.
Step 7
8
ClickRun ⌘

Click the 'Run' button (keyboard shortcut: Cmd+Enter) to execute the converted Stagehand script in a live browser session on Browserbase. The script will run in a real browser environment and output results to the console.

Tip. Monitor the console output for any errors or unexpected behavior. The first run may take a few seconds as Browserbase provisions a browser instance.
Warning. If the script performs web interactions (clicks, form submissions, etc.), ensure they are intended and safe to execute, as they will run against live websites.
Step 8

Confirm it worked

  1. 1The Browserbase Playground loads successfully with the code editor visible
  2. 2Your Playwright script appears in the editor after pasting
  3. 3A 'Convert to Stagehand' button is available in the playground interface
  4. 4The script executes without errors after clicking 'Run', with console output visible showing successful completion

Common issues

Keep reading

Convert to Stagehand Script | Browzer