For AI agents: a documentation index is available at /llms.txt
Skip to main content

Python (Playwright)

Playwright for Python connects to a remote browser with chromium.connect_over_cdp(). Point it at a Browserless wss:// endpoint with your API token and your existing script runs in the cloud. This page covers the sync and async APIs, stealth, proxies, and launch parameters.

Prerequisites
  • A Browserless API token from your account dashboard
  • Python 3.9+ with Playwright installed (pip install playwright). You don't need playwright install, since the browser runs on Browserless.

Sync API​

from playwright.sync_api import sync_playwright

TOKEN = "YOUR_API_TOKEN_HERE" # Replace with your actual token.

with sync_playwright() as p:
# connect_over_cdp works with any Playwright version, so you don't have to match the server's.
browser = p.chromium.connect_over_cdp(
f"wss://production-sfo.browserless.io?token={TOKEN}"
)
# Use the default context. new_context() doesn't inherit launch settings.
context = browser.contexts[0]
page = context.new_page()
page.goto("https://www.example.com/")
print(f"The page's title is: {page.title()}")
# Closing the browser ends the session, so you stop using session time.
browser.close()

Output

The page's title is: Example Domain

Async API​

Use the async API when your code already runs in an event loop (FastAPI, an agent framework) or when you want several pages in flight at once:

import asyncio
from playwright.async_api import async_playwright

TOKEN = "YOUR_API_TOKEN_HERE" # Replace with your actual token.

async def main():
async with async_playwright() as p:
browser = await p.chromium.connect_over_cdp(
f"wss://production-sfo.browserless.io?token={TOKEN}"
)
context = browser.contexts[0]
page = await context.new_page()
await page.goto("https://www.example.com/")
print(f"The page's title is: {await page.title()}")
await browser.close()

asyncio.run(main())

Output

The page's title is: Example Domain

Pick a region​

production-sfo is San Francisco. Use the region closest to the sites you automate, or to your own servers, to cut latency. See connection URL patterns for every region and query parameter.

Stealth and residential proxies​

Sites with bot detection block default headless Chrome. Swap the path to /stealth and add proxy=residential to route through a residential IP:

browser = p.chromium.connect_over_cdp(
f"wss://production-sfo.browserless.io/stealth?token={TOKEN}&proxy=residential&proxyCountry=us"
)

Residential proxies bill by data transferred, so block images and media you don't need. See stealth routes and proxies for the full options, including datacenter proxies.

Passing launch parameters​

Pass browser settings as a JSON launch query parameter. URL-encode it, because the JSON contains characters that break the query string otherwise:

import json
import urllib.parse
from playwright.sync_api import sync_playwright

TOKEN = "YOUR_API_TOKEN_HERE" # Replace with your actual token.

launch = json.dumps({"args": ["--window-size=1280,720"]})

with sync_playwright() as p:
browser = p.chromium.connect_over_cdp(
f"wss://production-sfo.browserless.io?token={TOKEN}&launch={urllib.parse.quote(launch)}"
)
page = browser.contexts[0].new_page()
page.goto("https://www.example.com/")
# Read the size from the page. The CDP default context has no fixed viewport of its own.
print(page.evaluate("[window.outerWidth, window.outerHeight]"))
browser.close()

Output

[1280, 720]

See launch parameters for everything you can set.

Native Playwright protocol​

connect_over_cdp() works with Chromium and Chrome. For Firefox, or to use Playwright's own protocol, call connect() on the matching browser type with the /playwright path. The native protocol needs your Playwright client version to match the server's:

browser = p.firefox.connect(
f"wss://production-sfo.browserless.io/firefox/playwright?token={TOKEN}"
)

FAQ & Troubleshooting​

Why am I getting a 403 Forbidden or 401 error?

Your API token is missing or wrong. Pass it as the ?token= query parameter in the wss:// URL and check it in your account dashboard.

My launch settings (window size, proxy) are ignored

You're probably calling browser.new_context(). A new context doesn't inherit launch settings, so use browser.contexts[0] as the examples above do.

Browser-level options such as headless are fixed when the browser starts, so no context choice changes them. Set them in the connection URL before you connect, as &headless=false or inside the launch parameter. See launch parameters.

browserType.connect fails with a version mismatch

The native protocol needs your Playwright client to match the server's version. Use connect_over_cdp() instead, which works with any version, or pin your playwright package to the server's version.

Is this the same as the old chrome.browserless.io endpoint?

No. chrome.browserless.io was the BaaS v1 endpoint. Use the regional production-*.browserless.io endpoints shown on this page.

Next steps​

Was this page helpful?