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:
Get Your API Token
- Go to the Browserless dashboard
- Sign up or log in to your account
- Copy your API token from the dashboard
Install Puppeteer
Install the required Puppeteer library for your language:
- Javascript
- Python
npm install puppeteer-core
pip install pyppeteer
Run Your First Example
Here's a complete working example that gets the title of a webpage:
- Javascript
- Python
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);import asyncio
from pyppeteer import connect_over_cdp
import asyncio
from pyppeteer import connect
TOKEN = "YOUR_API_TOKEN_HERE"
async def get_page_title():
# Connect to Browserless using WebSocket endpoint
browser = await connect({
"browserWSEndpoint": f"wss://production-sfo.browserless.io/?token={TOKEN}"
})
page = await browser.newPage()
await page.goto("https://www.example.com/")
title = await page.title()
print(f"The page's title is: {title}")
await browser.close()
if __name__ == "__main__":
asyncio.run(get_page_title())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
- Console output:
Next Steps
Explore these key features to enhance your browser automation: