Skip to main content

A Python client for the Chrome DevTools Protocol (CDP) optimized for DX.

Project description

cdpify

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

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

Listen to typed CDP events using async iterators:

from cdpify import CDPClient
from cdpify.domains import PageClient
from cdpify.domains.page.events import ScreencastFrameEvent

async def main():
    async with CDPClient("ws://localhost:9222/devtools/page/...") as client:
        page = PageClient(client)
        await page.enable()

        # Start screencast
        await page.start_screencast(format="jpeg", quality=80)

        # Listen to events with full type safety
        async for frame in client.listen("Page.screencastFrame", ScreencastFrameEvent):
            print(f"Frame received: {frame.data}")
            await page.screencast_frame_ack(
                screencast_frame_ack_session_id=frame.session_id
            )

asyncio.run(main())

Events are automatically deserialized into typed Pydantic models with full IDE support.

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, screenshots
  • RuntimeClient - JavaScript execution, console, objects
  • NetworkClient - Network monitoring, request interception
  • DOMClient - DOM tree access and manipulation
  • DebuggerClient - JavaScript debugging
  • EmulationClient - Device emulation, geolocation
  • PerformanceClient - Performance metrics
  • SecurityClient - 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 cdpify.generator

This downloads the latest protocol definitions and generates:

  • pydantic_cpd/domains/*/types.py - Type definitions
  • pydantic_cpd/domains/*/commands.py - Command parameters and results
  • pydantic_cpd/domains/*/events.py - Event definitions
  • pydantic_cpd/domains/*/client.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 concept of automatic code generation from the CDP specification is inspired by cdp-use.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cdpify-0.2.6.tar.gz (183.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cdpify-0.2.6-py3-none-any.whl (303.7 kB view details)

Uploaded Python 3

File details

Details for the file cdpify-0.2.6.tar.gz.

File metadata

  • Download URL: cdpify-0.2.6.tar.gz
  • Upload date:
  • Size: 183.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.2

File hashes

Hashes for cdpify-0.2.6.tar.gz
Algorithm Hash digest
SHA256 285bf6c9e69a1b0c586b4e8787a43be3ee06aa0f1a86b589b274bcd6f47ba85c
MD5 54af6448fc4d99344783661d9fc0f553
BLAKE2b-256 693b3b15079c7a3c613570fbbbcdbf6d4446ba2c8e1dbce44f22a676cdab7a4a

See more details on using hashes here.

File details

Details for the file cdpify-0.2.6-py3-none-any.whl.

File metadata

  • Download URL: cdpify-0.2.6-py3-none-any.whl
  • Upload date:
  • Size: 303.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.2

File hashes

Hashes for cdpify-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 f4d5c38005a73f1b4dba65093db7c04a741f8b205b5e7dd56dec1b43a1a96f71
MD5 46bd274332669c9a1bbf76e74b9913e8
BLAKE2b-256 6cac97b0e2940afeb2afa2eb5523517e5d009ec8da5ca62427fc3f96b4bfa835

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page