Complete Developer's Guide to Cloud Playwright & WebSocket CDP
Running headless browsers in production is notoriously challenging. This guide covers how to bypass local binary installations and connect Playwright directly to remote, pre-warmed Chromium instances over the Chrome DevTools Protocol (CDP).
Table of Contents
1. The Challenge of Running Headless Chrome Locally
2. Connecting via Playwright Python
import asyncio
from playwright.async_api import async_playwright
async def run_remote_browser():
async with async_playwright() as p:
# Connect to pre-warmed cloud Chromium instance over CDP
endpoint = "wss://agently.jobdriver.org/ws?token=YOUR_API_KEY"
print("Connecting to cloud Playwright session...")
browser = await p.chromium.connect_over_cdp(endpoint)
context = browser.contexts[0] if browser.contexts else await browser.new_context()
page = context.pages[0] if context.pages else await context.new_page()
# Execute actions exactly as you would with a local browser
await page.goto("https://news.ycombinator.com", wait_until="networkidle")
title = await page.title()
print(f"Page title retrieved from cloud browser: {title}")
# Capture a high-res screenshot
await page.screenshot(path="hn_cloud.png", full_page=True)
# Always close the session to release resources back to the pool
await browser.close()
if __name__ == "__main__":
asyncio.run(run_remote_browser())3. Connecting via Playwright Node.js / TypeScript
import { chromium } from "playwright-core";
async function executeCloudScrape() {
const wsEndpoint = "wss://agently.jobdriver.org/ws?token=YOUR_API_KEY";
const browser = await chromium.connectOverCDP(wsEndpoint);
try {
const context = browser.contexts()[0] || (await browser.newContext());
const page = context.pages()[0] || (await context.newPage());
await page.goto("https://example.com", { waitUntil: "domcontentloaded" });
const content = await page.innerText("h1");
console.log("Extracted H1:", content);
} finally {
// Explicitly closing terminates the cloud container session cleanly
await browser.close();
}
}
executeCloudScrape().catch(console.error);4. Production Best Practices for Cloud Browsers
Frequently Asked Questions
Does Agently support full Chrome DevTools Protocol commands?
Yes. Because Agently proxies standard Chromium CDP connections over WebSocket, you have full access to low-level CDP domains including Network, Page, Runtime, DOM, and Emulation.
What happens if my script crashes without calling browser.close()?
Agently enforces automatic zombie cleanup. If a WebSocket disconnects or remains idle without CDP activity for more than 30 seconds, the cloud container and all associated processes are automatically destroyed.
How many concurrent browser sessions can I run?
The Free Tier includes up to 2 concurrent sessions with 10 total browser hours. Paid plans offer unlimited concurrency with dedicated pre-warmed instance pools.
Related Tools & APIs
Live Browser Sandbox
Connect Playwright, Puppeteer, and autonomous AI agents to hosted cloud Chromium sessions over native CDP. Instant launch with 10 free hours.
Website Screenshot
Capture high-resolution PNG and JPEG full-page screenshots of dynamic websites with our cloud Playwright API. 1,000 free requests/month.
Scrape to JSON
Scrape websites into structured JSON records with CSS selectors, OpenGraph, and JSON-LD schema parsing.
Start Automating with Cloud Playwright Today
Skip Docker setup, memory leak bugs, and anti-bot arms races. Connect your Playwright or AI agent scripts in 30 seconds with zero credit card required.