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 Common errors
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. Warnings
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).
Imports
- Page wrong
import chrome_devtools_protocol.page.Pagecorrectfrom cdp.page import Page - Network wrong
from chrome_debug import Networkcorrectfrom cdp.network import Network
Quickstart
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())