Browsa 示範如何只改一行,讓 Playwright 腳本在雲端瀏覽器執行
Run your Playwright scripts in a cloud browser by changing one line
Browsa 示範只改一行,將 Playwright 從本機啟動 Chromium 改為透過 CDP 連接雲端瀏覽器,原有頁面操作程式碼可維持不變。連線需使用 cdp_http 和 X-API-Key,並沿用瀏覽器已有的 context;示例執行花費 1 credit,未用完的預留時間會退還。
Posted on Nov 24 Edited on Nov 24
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()
After
browser = p.chromium.connect_over_cdp(cdp_http, headers={"X-API-Key": API_KEY})
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
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
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
It cost 1 credit.
Three things that trip people up
-
Use
cdp_http, notcdp_url. The create response has both.cdp_httpis the browser endpoint Playwright and Puppeteer expect.cdp_urlis a single page's WebSocket, for clients that speak raw CDP. I got this wrong myself the first time: Playwright answers401 MISSING_API_KEYon the page socket. -
Use the existing context. The browser starts with a context and a page; use
browser.contexts[0]rather than creating a new one. -
Always destroy the browser. The
finallyblock 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 },
});
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/tasksand get JSON back.
The guide with every option: Playwright and Puppeteer over CDP.
來源:DEV Community · dev.to