Skip to main content
Version: v2

Connecting Playwright

Browserless supports Playwright's Chrome DevTools Protocol (CDP) connection method, which provides a reliable way to connect to Browserless. This method is similar to how Puppeteer operates and is well-suited for most automation tasks.

Follow these steps to get started with Playwright and Browserless:

  1. Get Your API Token

  2. Install Playwright

    Install the required Playwright library for your language:

    npm install playwright-core
  3. Run Your First Example

    Here's a complete working example that takes a screenshot of a website:

    import { chromium } from "playwright-core";

    const TOKEN = "YOUR_API_TOKEN_HERE"; // Replace with your actual token

    async function takeScreenshot() {
    // Connect to Browserless using CDP (recommended method)
    const browser = await chromium.connectOverCDP(
    `wss://production-sfo.browserless.io?token=${TOKEN}`
    );

    const context = await browser.newContext();
    const page = await context.newPage();

    // Navigate to the target website
    await page.goto("https://www.example.com/");

    // Take a screenshot and save it locally
    await page.screenshot({ path: "screenshot.png" });
    console.log("Screenshot saved as screenshot.png");

    // Clean up resources
    await browser.close();
    }

    takeScreenshot().catch(console.error);
  4. Expected Output

    After running your script, you should see:

    • A screenshot file named screenshot.png created in your current directory
    • Console output: "Screenshot saved as screenshot.png"

Next Steps

Explore these key features to enhance your browser automation: