Python wrapper for Chrome DevTools Protocol.
pip install chromedeveloperprotocol-clientThis example shows how to connect to a running browser and fetch its version information.
import asyncio
import httpx
from cdp import Client
async def get_ws_url(port=9222):
async with httpx.AsyncClient() as http:
resp = await http.get(f"http://localhost:{port}/json/version")
return resp.json()['webSocketDebuggerUrl']
async def main():
# Connect to an existing browser session
# Browser must be started with --remote-debugging-port=9222
ws_url = await get_ws_url()
async with Client(ws_url) as client:
# Fetch browser version information
version = await client.browser.get_version({})
print(f"Browser: {version['product']}")
# Create a new page and navigate
target = await client.target.create_target({"url": "https://www.google.com"})
print(f"Created target: {target['targetId']}")
if __name__ == "__main__":
asyncio.run(main())Ensure you have a browser (Chrome or Edge) running with remote debugging enabled:
chrome.exe --remote-debugging-port=9222