CDP-Use: Chrome DevTools Protocol Client

1.4.5 · active · verified Fri Apr 10

CDP-Use is a type-safe Python client library for the Chrome DevTools Protocol (CDP). It provides auto-generated, strongly-typed commands and events, allowing for interaction with browsers like Chrome and Edge, or browser automation tools like Playwright and Puppeteer. It is actively maintained with regular releases, currently at version 1.4.5.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a browser's CDP endpoint, navigate to a URL, and fetch some basic page information. Ensure you have a browser running with remote debugging enabled (e.g., `chrome --remote-debugging-port=9222`).

import asyncio
from cdp_use import CDP
from cdp_use.target import Target
import os

async def main():
    # Ensure Chrome/Edge is running with --remote-debugging-port=9222
    # or connect to a Playwright/Puppeteer launched browser.
    # The URL can be an HTTP endpoint ('http://...') or a WebSocket URL ('ws://...')
    cdp_url = os.environ.get("CDP_DEBUGGING_URL", "http://127.0.0.1:9222")
    
    async with CDP(Target(cdp_url)) as cdp:
        page = cdp.page
        await page.enable()
        print(f"Navigating to example.com via CDP...")
        await page.navigate("https://www.example.com")
        
        # Wait for navigation to complete (e.g., load event)
        await page.await_load_event()
        
        current_url = await page.get_navigated_history().result.current_entry.url
        print(f"Current URL: {current_url}")
        
        # Example: Get document layout metrics
        layout_metrics = await page.get_layout_metrics()
        print(f"Document content width: {layout_metrics.content_size.width}")

        await asyncio.sleep(2) # Keep the connection alive briefly for inspection

asyncio.run(main())

view raw JSON →