Puppeteer (Node.js)

Introduction

This guide provides a step-by-step example of how to connect to a remote browser using the Puppeteer library for Node.js. It includes instructions on how to use an API token for authentication when connecting to CloudBrowser.

Prerequisites

  • Node.js: Ensure Node.js is installed on your system.

  • CloudBrowserConnector Library: Install Puppeteer by running npm install cloud-browser-connector.

  • API Token: Your unique API token for authenticating requests to CloudBrowser.

Example Code

The following code demonstrates how to connect to a remote browser using Puppeteer, retrieve the WebSocket endpoint with an API token, and perform basic navigation.

const CloudBrowserConnector = require('cloud-browser-connector');

const apiToken = '3341fg352-3134-4a4b-4311-b93983f068ae';

const options = {
    Headless: false,
};

const cloudBrowser = new CloudBrowserConnector(apiToken, options);

// Run your tasks
const browser = await cloudBrowser.connect();

const page = await browser.newPage();
await page.goto('https://example.com');

In summary, this code:

  • Connects to a remote browser using CloudBrowser.

  • Opens a new page in that browser session.

  • Then navigates to a given URL.

Ensure the browser is closed after operations are complete.

await browser.close();

Error Handling

  • HTTP Request Errors: Handle errors when fetching the WebSocket URL by checking the response status and throwing an error if the request fails.

  • Connection Errors: Catch and log errors that occur during connection to the remote browser or during navigation.

Last updated