import asyncio import json import websockets import requests async def test_cdp(): pages = requests.get("http://localhost:9222/json/list").json() ws_url = pages[0]["webSocketDebuggerUrl"] print("Connecting to:", ws_url) async with websockets.connect(ws_url, max_size=50*1024*1024) as ws: await ws.send(json.dumps({"id": 1, "method": "Page.enable"})) print("Page.enable:", await ws.recv()) # Check window/document size await ws.send(json.dumps({ "id": 2, "method": "Runtime.evaluate", "params": { "expression": "({ width: document.documentElement.scrollWidth, height: document.documentElement.scrollHeight, innerH: window.innerHeight, innerW: window.innerWidth })", "returnByValue": True } })) print("Doc size:", await ws.recv()) # Test screenshot await ws.send(json.dumps({ "id": 3, "method": "Page.captureScreenshot", "params": { "format": "png", "fromSurface": True } })) res = json.loads(await ws.recv()) print("Shot result keys:", res.keys()) if "error" in res: print("Shot error:", res["error"]) elif "result" in res: print("Shot result data len:", len(res["result"].get("data", ""))) if __name__ == "__main__": asyncio.run(test_cdp())