Add your description here
Project description
Pydantic CDP
Type-safe Python client for the Chrome DevTools Protocol (CDP) with Pydantic validation.
Installation
pip install cdpify
What it does
This library provides Python bindings for the Chrome DevTools Protocol with full type safety through Pydantic models. All CDP domains, commands, events, and types are automatically generated from the official Chrome DevTools Protocol specifications.
Usage
Basic Connection
import asyncio
from cdpify import CDPClient
async def main():
async with CDPClient("ws://localhost:9222/devtools/browser/...") as client:
# Send CDP commands
result = await client.send_raw(
method="Target.getTargets",
params=None
)
print(f"Targets: {result}")
asyncio.run(main())
Using Domain Clients
Domain-specific clients provide typed methods for all CDP commands:
from cdpify import CDPClient
from cdpify.domains import PageClient, RuntimeClient
async def main():
async with CDPClient("ws://localhost:9222/devtools/page/...") as client:
# Initialize domain clients
page = PageClient(client)
runtime = RuntimeClient(client)
# Navigate to a page
await page.navigate(url="https://example.com")
# Evaluate JavaScript
result = await runtime.evaluate(
expression="document.title",
return_by_value=True
)
print(f"Page title: {result.result.value}")
asyncio.run(main())
Event Handling
Register handlers for CDP events:
from cdpify import CDPClient
from cdpify.domains import PageClient
async def main():
client = CDPClient("ws://localhost:9222/devtools/page/...")
await client.connect()
# Register event handler
@client.on("Page.loadEventFired")
async def on_load(params, session_id):
print(f"Page loaded at timestamp: {params['timestamp']}")
# Wildcard handler for all events
@client.on()
async def on_any_event(params, session_id):
print(f"Event received: {params}")
page = PageClient(client)
await page.enable()
await page.navigate(url="https://example.com")
await asyncio.sleep(5)
await client.disconnect()
asyncio.run(main())
Configuration
client = CDPClient(
url="ws://localhost:9222/devtools/browser/...",
additional_headers={"Authorization": "Bearer token"},
max_frame_size=100 * 1024 * 1024, # 100MB
default_timeout=30.0 # seconds
)
Available Domain Clients
All CDP domains are available as typed clients:
PageClient- Page lifecycle, navigation, screenshotsRuntimeClient- JavaScript execution, console, objectsNetworkClient- Network monitoring, request interceptionDOMClient- DOM tree access and manipulationDebuggerClient- JavaScript debuggingEmulationClient- Device emulation, geolocationPerformanceClient- Performance metricsSecurityClient- Security state, certificates- And 40+ more domains...
Import them from the root package:
from cdpify import (
CDPClient,
PageClient,
NetworkClient,
RuntimeClient,
# ... all other domain clients
)
Type Safety
All commands and events use Pydantic models for validation:
# Parameters are validated
await page.navigate(
url="https://example.com",
referrer="https://google.com", # Optional parameter
transition_type="link" # Validated against allowed values
)
# Return values are typed
result = await runtime.evaluate(expression="1 + 1")
print(result.result.value) # Pydantic model with full IDE support
Code Generation
The CDP bindings are generated from the official Chrome DevTools Protocol specifications. To regenerate:
uv run python -m pydantic_cpd.generator
This downloads the latest protocol definitions and generates:
pydantic_cpd/domains/*/types.py- Type definitionspydantic_cpd/domains/*/commands.py- Command parameters and resultspydantic_cpd/domains/*/events.py- Event definitionspydantic_cpd/domains/*/library.py- Domain client classes
Project Structure
pydantic_cpd/
├── client.py # Core CDP WebSocket client
├── events.py # Event dispatcher
├── exceptions.py # CDP exceptions
├── domains/ # Generated CDP bindings
│ ├── page/
│ ├── runtime/
│ ├── network/
│ └── ... (42 domains)
└── generator/ # Code generation tools
Requirements
- Python 3.14+
- pydantic >= 2.12
- websockets >= 15.0
- httpx >= 0.28
Inspiration
The idea for automatic code generation and the overall approach is inspired by cdp-use. This project adapts that concept for Python, with a slightly different API design and type system using Pydantic models.
Links
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cdpify-0.1.1.tar.gz.
File metadata
- Download URL: cdpify-0.1.1.tar.gz
- Upload date:
- Size: 142.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7795f5950ead98bff05495dc2ca3d24a1ae0084a9a1d97c849b5b1ae9d0c6682
|
|
| MD5 |
cab9e4f1109d0c0332527209a836be84
|
|
| BLAKE2b-256 |
3b29b81a0a5e4faf896fd709e707171f47da47a42779d63992048581accf888e
|
File details
Details for the file cdpify-0.1.1-py3-none-any.whl.
File metadata
- Download URL: cdpify-0.1.1-py3-none-any.whl
- Upload date:
- Size: 231.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb2140ea9d4817891fbefa6e1b93866b3d54b1597f3f04c24d6f153f5db37b5f
|
|
| MD5 |
0a678bb33e25bd046c3c2394947ef8b6
|
|
| BLAKE2b-256 |
6f68bfe152c85350bcb2a4f9875db36072a3895683e88623fd1f57f36f8f62cf
|