Connect to a browser

Connect Playwright or Puppeteer using the protocol returned by Browser API.

Match the client to the returned protocol

Chromium sessions return cdp; Firefox sessions return bidi. Connect before
connection.expires_at and do not alter the WebSocket URL.

Chromium with Playwright

import { chromium } from "playwright";

const browser = await chromium.connectOverCDP(process.env.BROWSER_URL);
try {
  const context = browser.contexts()[0];
  if (!context) throw new Error("Browser context unavailable");

  const page = await context.newPage();
  await page.goto("https://example.com", {
    waitUntil: "domcontentloaded",
    timeout: 30_000,
  });
  console.log(await page.title());
} finally {
  await browser.close();
}

Firefox with Puppeteer

import { connect } from "puppeteer-core";

const browser = await connect({
  browserWSEndpoint: process.env.BROWSER_URL,
  protocol: "webDriverBiDi",
});
try {
  const page = await browser.newPage();
  await page.goto("https://example.com", { waitUntil: "domcontentloaded" });
  console.log(await page.title());
} finally {
  browser.disconnect();
}

Closing an automation client does not always terminate the remote session. Send
DELETE /sessions/{session_id}/ when the session is no longer needed.


Did this page help you?