Skip to main content

Connecting Puppeteer

Puppeteer can connect to Browserless through Chrome's DevTools Protocol (CDP) using websockets. This is the primary and recommended way to connect to Browserless, as it provides a stable and reliable connection.

Follow these steps to get started with Puppeteer and Browserless:

  1. Get Your API Token

  2. Install Puppeteer

    Install the required Puppeteer library for your language:

    npm install puppeteer-core
  3. Run Your First Example

    Here's a complete working example that gets the title of a webpage:

    import puppeteer from "puppeteer-core";

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

    async function getPageTitle() {
    // Connect to Browserless using WebSocket endpoint
    const browser = await puppeteer.connect({
    browserWSEndpoint: `wss://production-sfo.browserless.io?token=${TOKEN}`,
    });

    const page = await browser.newPage();

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

    // Get the page title
    const title = await page.title();
    console.log(`The page's title is: ${title}`);

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

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

    After running your script, you should see:

    • Console output: "The page's title is: Example Domain" (or similar)
    • Successful connection to Browserless
    • Clean browser session termination

Next Steps

Explore these key features to enhance your browser automation: