import asyncio
import boxlite
# Connecting to the in-box browser requires the playwright client library on this machine
# pip install "playwright==1.58.0" # must match the in-box Playwright Server
from playwright.async_api import async_playwright
async def main():
try:
# Default chromium, 2 CPU / 2048 MiB; the box only starts on entering async with
async with boxlite.BrowserBox() as browser:
# Start the Playwright Server inside the box, return a host-connectable WebSocket endpoint
ws_endpoint = await browser.playwright_endpoint()
print(f"Ready, endpoint: {ws_endpoint}") # e.g. ws://localhost:3000/
async with async_playwright() as p:
# Connect from the host to the browser inside the box
browser_ctx = await p.chromium.connect(ws_endpoint)
page = await browser_ctx.new_page()
await page.goto("https://example.com")
print("title:", await page.title())
await browser_ctx.close()
except Exception as exc:
# Image pull failure, no virtualization, etc. raise standard exceptions; catch-all here
print(f"BrowserBox failed: {type(exc).__name__}: {exc}")
if __name__ == "__main__":
asyncio.run(main())