Playwright
BrowserGen sessions expose a standard Chrome DevTools Protocol endpoint, so Playwright attaches with its built-in connectOverCDP. No plugin, no custom launcher.
Connect
import { chromium } from "playwright";
const BROWSERGEN_KEY = process.env.BROWSERGEN_KEY!;
// 1. Create a session over REST.
const session = await fetch("https://api.browsergen.com/v8/web/sessions", {
method: "POST",
headers: {
Authorization: `Bearer ${BROWSERGEN_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ timeout: 1800, country: "us" }),
}).then((r) => r.json());
// 2. Attach Playwright to the running browser.
const browser = await chromium.connectOverCDP(session.connectUrl);
// 3. Reuse the browser's existing context and page.
const context = browser.contexts()[0];
const page = context.pages()[0] ?? (await context.newPage());
await page.goto("https://example.com");
await page.screenshot({ path: "example.png" });
// 4. Disconnect. The session ends ~2 minutes later, or stop it via DELETE.
await browser.close();Three details worth knowing:
- Use the existing context. The browser is already running with one context and one page. Reusing them keeps your script attached to what the live view shows.
browser.close()disconnects, it does not stop. The session stays alive for a 2-minute grace period after the last client disconnects, then ends on its own. SendDELETE /web/sessions/:idwhen you want the slot back immediately.- Multiple clients are fine. Several Playwright processes can attach to the same session concurrently, and the live view stays available throughout.
Viewports and devices
// The session runs at a fixed screen size (that is what the live view
// streams). Emulate a different viewport per page over CDP if you need one:
const cdp = await context.newCDPSession(page);
await cdp.send("Emulation.setDeviceMetricsOverride", {
width: 390,
height: 844,
deviceScaleFactor: 3,
mobile: true,
});Python
The same flow with playwright-python:
from playwright.sync_api import sync_playwright
import os, requests
session = requests.post(
"https://api.browsergen.com/v8/web/sessions",
headers={"Authorization": f"Bearer {os.environ['BROWSERGEN_KEY']}"},
json={"country": "de"},
).json()
with sync_playwright() as p:
browser = p.chromium.connect_over_cdp(session["connectUrl"])
page = browser.contexts[0].pages[0]
page.goto("https://example.com")
print(page.title())
browser.close()