Skip to content

SOP 002: Connect with Playwright

Fresh
FieldValue
SOP IDSOP-002
Version1.0
StatusActive
SourcePlaywright Guide

Overview

Connect Playwright to Steel sessions for browser automation. Two methods available: one-line change (simplest) or create-and-connect (full control).

Prerequisites

  • steel-sdk and playwright installed
  • Steel API key configured

Connection Methods

Method 1: One-Line Change (Easiest)

Replace your existing chromium.launch() with:

typescript
const browser = await chromium.connectOverCDP(
  'wss://connect.steel.dev?apiKey=MY_STEEL_API_KEY'
);

This auto-creates a session with default settings. Session releases when you call browser.close().

Custom Session ID

typescript
import { v4 as uuidv4 } from 'uuid';

const sessionId = uuidv4();
const browser = await chromium.connectOverCDP(
  `wss://connect.steel.dev?apiKey=${process.env.STEEL_API_KEY}&sessionId=${sessionId}`
);

Method 2: Create and Connect (Full Control)

typescript
import Steel from 'steel-sdk';
import { chromium } from 'playwright';

const client = new Steel({
  steelAPIKey: process.env.STEEL_API_KEY,
});

async function main() {
  // Create session with features
  const session = await client.sessions.create({
    useProxy: true,
    solveCaptcha: true,
  });

  // Connect Playwright
  const browser = await chromium.connectOverCDP(
    `wss://connect.steel.dev?apiKey=${process.env.STEEL_API_KEY}&sessionId=${session.id}`
  );

  // Use existing context for session recording
  const page = browser.contexts()[0].pages()[0];
  await page.goto('https://example.com');

  // Clean up
  await browser.close();
  await client.sessions.release(session.id);
}

main();

Important

Use browser.contexts()[0].pages()[0] instead of creating a new context. This ensures the session is recorded in the live viewer.

Verification Checklist

  • [ ] Playwright connects via WebSocket without errors
  • [ ] Page navigation works (page.goto())
  • [ ] Session visible in live viewer
  • [ ] Session properly released after automation

Troubleshooting

IssueSolution
Connection refusedCheck API key and session ID format
Session not recorded in viewerUse existing context, don't create new one
Timeout on connectIncrease connection timeout, check network

See Also

SOP Documentation Hub - Built from Steel.dev Official Docs