Oven logo

Oven

cdp-use1.4.1

Published

Type safe generator/client library for CDP

pip install cdp-use

Package Downloads

Weekly DownloadsMonthly Downloads

Authors

Requires Python

>=3.11

CDP Use

A type-safe Python client generator for the Chrome DevTools Protocol (CDP). This library automatically generates Python bindings with full TypeScript-like type safety from the official CDP protocol specifications.

šŸš€ Features

  • šŸ”’ Type Safety: Full type hints with TypedDict classes for all CDP commands, parameters, and return types
  • šŸŽÆ Event Registration: Typesafe event handlers with full IDE support
  • šŸ—ļø Auto-Generated: Code generated directly from official Chrome DevTools Protocol specifications
  • šŸŽÆ IntelliSense Support: Perfect IDE autocompletion and type checking
  • šŸ“¦ Domain Separation: Clean organization with separate modules for each CDP domain
  • šŸ”„ Always Up-to-Date: Easy regeneration from latest protocol specs

šŸ› ļø Installation & Setup

  1. Clone and install dependencies:
git clone https://github.com/browser-use/cdp-use
cd cdp-use
uv sync  # or pip install -r requirements.txt
  1. Generate the CDP client library:
python -m cdp_use.generator

This automatically downloads the latest protocol specifications and generates all type-safe bindings.

šŸ“– Usage Examples

Basic Usage

import asyncio
from cdp_use.client import CDPClient

async def main():
    # Connect to Chrome DevTools
    async with CDPClient("ws://localhost:9222/devtools/browser/...") as cdp:
        # Get all browser targets with full type safety
        targets = await cdp.send.Target.getTargets()
        print(f"Found {len(targets['targetInfos'])} targets")

        # Navigate to a page
        await cdp.send.Page.navigate({"url": "https://example.com"})

asyncio.run(main())

Type Safety in Action

# āœ… Fully typed parameters
await cdp.send.Runtime.evaluate(params={
    "expression": "document.title",
    "returnByValue": True
})

# āœ… Return types are fully typed
result = await cdp.send.DOM.getDocument(params={"depth": 1})
node_id: int = result["root"]["nodeId"]  # Full IntelliSense support

# āŒ Type errors caught at development time
await cdp.send.DOM.getDocument(params={"invalid": "param"})  # Type error!

šŸŽ§ Event Registration

The library provides typesafe event registration with full IDE support:

Basic Event Registration

import asyncio
from cdp_use.client import CDPClient
from cdp_use.cdp.page.events import FrameAttachedEvent, DomContentEventFiredEvent
from cdp_use.cdp.runtime.events import ConsoleAPICalledEvent
from typing import Optional

def on_frame_attached(event: FrameAttachedEvent, session_id: Optional[str]) -> None:
    print(f"Frame {event['frameId']} attached to {event['parentFrameId']}")

def on_dom_content_loaded(event: DomContentEventFiredEvent, session_id: Optional[str]) -> None:
    print(f"DOM content loaded at: {event['timestamp']}")

def on_console_message(event: ConsoleAPICalledEvent, session_id: Optional[str]) -> None:
    print(f"Console: {event['type']}")

async def main():
    async with CDPClient("ws://localhost:9222/devtools/page/...") as client:
        # Register event handlers with camelCase method names (matching CDP)
        client.register.Page.frameAttached(on_frame_attached)
        client.register.Page.domContentEventFired(on_dom_content_loaded)
        client.register.Runtime.consoleAPICalled(on_console_message)

        # Enable domains to start receiving events
        await client.send.Page.enable()
        await client.send.Runtime.enable()

        # Navigate and receive events
        await client.send.Page.navigate({"url": "https://example.com"})
        await asyncio.sleep(5)  # Keep listening for events

Event Registration Features

āœ… Type Safety: Event handlers are validated at compile time
āœ… IDE Support: Full autocomplete for domains and event methods
āœ… Parameter Validation: Callback signatures are type-checked
āœ… Event Type Definitions: Each event has its own TypedDict interface

Registration Syntax

client.register.Domain.eventName(callback_function)

Where:

  • Domain is any CDP domain (Page, Runtime, Network, etc.)
  • eventName is the camelCase CDP event name (matching CDP specs)
  • callback_function must accept (event_data, session_id) parameters

Available Event Domains

  • Page: client.register.Page.* - Page lifecycle, navigation, frames
  • Runtime: client.register.Runtime.* - JavaScript execution, console, exceptions
  • Network: client.register.Network.* - HTTP requests, responses, WebSocket
  • DOM: client.register.DOM.* - DOM tree changes, attributes
  • CSS: client.register.CSS.* - Stylesheet changes, media queries
  • Debugger: client.register.Debugger.* - Breakpoints, script parsing
  • Performance: client.register.Performance.* - Performance metrics
  • Security: client.register.Security.* - Security state changes
  • And many more...

Type Safety Examples

āœ… Correct Usage:

def handle_console(event: ConsoleAPICalledEvent, session_id: Optional[str]) -> None:
    print(f"Console: {event['type']}")

client.register.Runtime.consoleAPICalled(handle_console)

āŒ Type Error - Wrong signature:

def bad_handler(event):  # Missing session_id parameter
    pass

client.register.Runtime.consoleAPICalled(bad_handler)  # Type error!

šŸ“‹ What Gets Generated

cdp_use/cdp/
ā”œā”€ā”€ library.py                    # Main CDPLibrary class
ā”œā”€ā”€ registry.py                   # Event registry system
ā”œā”€ā”€ registration_library.py       # Event registration interface
ā”œā”€ā”€ dom/                          # DOM domain
│   ā”œā”€ā”€ types.py                 # DOM-specific types
│   ā”œā”€ā”€ commands.py              # Command parameter/return types
│   ā”œā”€ā”€ events.py                # Event types
│   ā”œā”€ā”€ library.py               # DOMClient class
│   └── registration.py          # DOM event registration
ā”œā”€ā”€ page/                         # Page domain
│   └── ...
└── ... (50+ domains total)

šŸ›ļø Architecture

Main Components

class CDPClient:
    def __init__(self, url: str):
        self.send: CDPLibrary                    # Send commands
        self.register: CDPRegistrationLibrary    # Register events

# Domain-specific clients
class CDPLibrary:
    def __init__(self, client: CDPClient):
        self.DOM = DOMClient(client)           # DOM operations
        self.Network = NetworkClient(client)   # Network monitoring
        self.Runtime = RuntimeClient(client)   # JavaScript execution
        # ... 50+ more domains

# Event registration
class CDPRegistrationLibrary:
    def __init__(self, registry: EventRegistry):
        self.Page = PageRegistration(registry)
        self.Runtime = RuntimeRegistration(registry)
        # ... all domains with events

šŸ”§ Development

Regenerating Types

python -m cdp_use.generator

This will:

  1. Download the latest protocol files from Chrome DevTools repository
  2. Generate all Python type definitions and event registrations
  3. Create domain-specific client classes
  4. Format the code

Project Structure

cdp-use/
ā”œā”€ā”€ cdp_use/
│   ā”œā”€ā”€ client.py              # Core CDP WebSocket client
│   ā”œā”€ā”€ generator/             # Code generation tools
│   └── cdp/                   # Generated CDP library (auto-generated)
ā”œā”€ā”€ simple.py                  # Example usage
└── README.md

šŸ¤ Contributing

  1. Fork the repository
  2. Make changes to generator code (not the generated cdp_use/cdp/ directory)
  3. Run python -m cdp_use.generator to regenerate
  4. Test with python simple.py
  5. Submit a pull request

šŸ”— Related


Generated from Chrome DevTools Protocol specifications • Type-safe • Zero runtime overhead