Connect with Playwright (Node)
Drive a Steel session with Playwright via WebSocket connection
This guide shows you how to drive Steel's cloud browser sessions using Playwright with Node.js/TypeScript. Looking for Python? Check out our Playwright Python guide.
Steel sessions are designed to be easily driven by Playwright. There are two main methods for connecting to & driving a Steel session with Playwright.
Quick Start: Want to jump right in? Skip to example project.
Method #1: One-line change (easiest)
Most Playwright scripts start with chromium.launch() function to launch your browser with desired args that looks something like this:
1const browser = await chromium.launch({...});
Simply change this line to the following (replacing MY_STEEL_API_KEY with your api key):
1const browser = await chromium.connectOverCDP(2'wss://connect.steel.dev?apiKey=MY_STEEL_API_KEY'3);
and voila! This will automatically start and connect to a Steel session for you with all default parameters set. Your subsequent calls will work as they did previously.
When you're done, the session automatically releases when your script calls browser.close(), browser.disconnect(), or ends the connection.
Advanced: Custom Session IDs
This doesn’t support other UTM parameters to add args (that is what Method #2 is for) other than one - sessionId. This allows you to set a custom session id (UUIDv4 format) for the session.
This is helpful because you don’t get any data returned from connecting like this but by setting your own session ID, you can use the API/SDKs to retrieve data or taking actions on the session like manually releasing it.
Example:
1import { v4 as uuidv4 } from 'uuid';2import Steel from 'steel-sdk';34const sessionId = uuidv4(); // '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'56const browser = await chromium.connectOverCDP(7`wss://connect.steel.dev?apiKey=${process.env.STEEL_API_KEY}&sessionId=${sessionId}`8);910// Get session details11const client = new Steel();12const session = await client.sessions.retrieve(sessionId);13console.log(`View session live at: ${session.sessionViewerUrl}`);
Method #2: Create and connect
Use this method when you need to drive a session with non-default features like proxy support or CAPTCHA solving. The main difference is that you'll:
-
Start a session via API
-
Connect to it via chromium.connectOverCDP()
-
Release the session when finished
If you want your session to be recorded in the live viewer make sure to use the existing browser context from the session when controlling a page as opposed to creating a new context.
1import Steel from 'steel-sdk';2import { chromium } from 'playwright';3import dotenv from 'dotenv';45dotenv.config();67const client = new Steel({8steelAPIKey: process.env.STEEL_API_KEY,9});1011async function main() {12// Create a session with additional features13const session = await client.sessions.create({14useProxy: true,15solveCaptcha: true,16});1718// Connect with Playwright19const browser = await chromium.connectOverCDP(20`wss://connect.steel.dev?apiKey=${process.env.STEEL_API_KEY}&sessionId=${session.id}`21);2223// Create page at existing context to ensure session is recorded. This is crucial!24const currentContext = browser.contexts()[0];25const page = await currentContext.pages()[0];2627// Run your automation28await page.goto('https://example.com');2930// Always clean up when done31await browser.close();32await client.sessions.release(session.id);33}3435main();
Important: With Method #2, sessions remain active until explicitly released or timed out. It’s best practise to call client.sessions.release() when finished instead of waiting for the session to timeout to be released.
Example Project: Scraping Hacker News
Here's a working example that scrapes Hacker News with proper error handling and session management:
Starter code that scrapes Hacker News for top 5 stories using Steel's Node SDK and Playwright.
Run by entering following commands in the terminal:
-
export STEEL_API_KEY=your_api_key -
npm start
The example includes:
-
Complete session configuration options
-
Error handling best practices
-
A working Hacker News scraper example
-
TypeScript support
You can also clone it on Github, StackBlitz, or Replit to start editing it yourself!