DEV Community

Cover image for Run your Playwright scripts in a cloud browser by changing one line
Browsa
Browsa

Posted on Edited on

Run your Playwright scripts in a cloud browser by changing one line

Disclosure: I work on Browsa, the cloud browser used here.

You have a Playwright script that works on your laptop. You want it to run somewhere else: a cron job, a server, an AI agent's tool. Shipping Chromium and its dependencies into every container is a chore, and a headless browser on a server IP gets blocked on plenty of sites.

The other option is to keep the script and point it at a browser that already runs in the cloud. In Playwright, that's one line.

Before

browser = p.chromium.launch()
Enter fullscreen mode Exit fullscreen mode

After

browser = p.chromium.connect_over_cdp(cdp_http, headers={"X-API-Key": API_KEY})
Enter fullscreen mode Exit fullscreen mode

Everything after that line (page.goto, locators, evaluate) stays the same.

The whole script

pip install browsa playwright
export BROWSA_API_KEY=agt_live_...   # app.browsa.io, 100 free credits, no card
Enter fullscreen mode Exit fullscreen mode

You don't need playwright install: no browser runs on your machine.

import os
from browsa import Client
from playwright.sync_api import sync_playwright

API_KEY = os.environ["BROWSA_API_KEY"]
client = Client(api_key=API_KEY)

agent = client.create_agent(ttl="10m")  # a cloud browser, billed per started 12 minutes
print("watch it live:", agent.get("live_url"))

try:
    with sync_playwright() as p:
        # The only line that changes: connect instead of launch
        browser = p.chromium.connect_over_cdp(
            agent["cdp_http"], headers={"X-API-Key": API_KEY}
        )
        context = browser.contexts[0]  # the browser already has a context and a page
        page = context.pages[0] if context.pages else context.new_page()

        page.goto("https://news.ycombinator.com")
        for link in page.locator(".titleline > a").all()[:5]:
            print("-", link.inner_text())

        browser.close()
finally:
    client.destroy_agent(agent["id"])  # unused time is refunded
Enter fullscreen mode Exit fullscreen mode

What it printed on 24 November 2026, 19 seconds from start to finish including the browser start:

watch it live: https://browsa.io/live/…
- Nvidia Reinstates Jensen Huang as CEO
- New York Bans Subscription and Post-Purchase Fees on Cars
- Stalwart is now a drop-in replacement for Microsoft Exchange
- Ladybird is now in alpha
- Drone Maps of Salvage Yards
Enter fullscreen mode Exit fullscreen mode

It cost 1 credit.

Three things that trip people up

  1. Use cdp_http, not cdp_url. The create response has both. cdp_http is the browser endpoint Playwright and Puppeteer expect. cdp_url is a single page's WebSocket, for clients that speak raw CDP. I got this wrong myself the first time: Playwright answers 401 MISSING_API_KEY on the page socket.
  2. Use the existing context. The browser starts with a context and a page; use browser.contexts[0] rather than creating a new one.
  3. Always destroy the browser. The finally block hands back the time you reserved but didn't use. If your script crashes, the TTL ends the browser anyway.

Puppeteer and Node

const browser = await puppeteer.connect({
  browserURL: agent.cdp_http,
  headers: { "X-API-Key": process.env.BROWSA_API_KEY },
});
Enter fullscreen mode Exit fullscreen mode

Where to go from here

  • Saved logins: create a persistent identity once and open sessions on it. Cookies and storage carry over.
  • Proxies: pass your own HTTP or SOCKS5 proxy at no charge, or use Browsa's network (metered per GB).
  • Skip the code: send a plain-language task to /v1/tasks and get JSON back.

The guide with every option: Playwright and Puppeteer over CDP.

Top comments (0)