Chrome DevTools Protocol

raw JSON →
0.4.0 verified Fri May 01 auth: no python

Python type wrappers for Chrome DevTools Protocol (CDP). Provides typed classes and enums for all CDP domains. Current version 0.4.0, updated infrequently.

pip install chrome-devtools-protocol
error ModuleNotFoundError: No module named 'chrome_devtools_protocol'
cause Incorrect import path. The actual module is 'cdp', not the package name.
fix
Use 'from cdp...' instead of 'from chrome_devtools_protocol...'.
error TypeError: Object of type Page is not JSON serializable
cause Sending a CDP command class directly instead of calling its .to_json() method or using str().
fix
Call the command as a function that returns a dict: await ws.send(Page.enable()) sends a JSON string automatically.
gotcha The package does not manage WebSocket connections. You must handle the WebSocket yourself (e.g., using websockets library).
fix Install 'websockets' and manage WebSocket lifecycle manually.
deprecated The 'event_parsers' module is deprecated; use message dispatch via domain events.
fix Refer to the cdp.event_dispatch or use domain event classes directly (e.g., from cdp.page import Page.domContentEventFired).

Connect to a CDP endpoint (e.g., Chrome debug port) and send a command.

import asyncio
from websockets import connect
from cdp import Page, Network

async def main():
    async with connect('ws://localhost:9222/devtools/browser/...') as ws:
        # Send a Page.enable command
        await ws.send(Page.enable())
        # Receive response
        response = await ws.recv()
        print(response)

asyncio.run(main())