CDP Use (Rust)
Rust implementation of cdp-use -- a type-safe Chrome DevTools Protocol client with auto-generated bindings from the official CDP protocol specifications.
Features
- Type-safe commands & events -- Strongly typed structs for all 53 CDP domains, generated at build time via
build.rs - Async WebSocket client -- Built on
tokio+tokio-tungstenitewith proper timeout, close ordering, and concurrent event dispatch - Typed event registration -- Register async closures for CDP events with full type safety
- Configurable connection -- Custom WebSocket frame size, headers, and command timeout via
CdpClientConfig - Zero manual maintenance -- Code generated directly from
browser_protocol.jsonandjs_protocol.json
Installation
Add to your Cargo.toml:
[]
= "0.1"
# or from git
= { = "https://github.com/netkn001/cdp-use-rs" }
Quick Start
use page;
async
Custom Configuration
use ;
use Duration;
let config = CdpClientConfig ;
let cdp = connect_with_config.await?;
Architecture
cdp-use-rs/
build.rs # Code generator -- reads protocol JSON, emits generated.rs
protocols/
browser_protocol.json
js_protocol.json
src/
lib.rs # Public API re-exports
client.rs # WebSocket client, event registry, CdpClientConfig
error.rs # CdpError enum
examples/
basic.rs # Full usage example
tests/
codegen_test.rs # Type/serialization/config tests
integration_test.rs # Live browser integration tests
At build time, build.rs parses the CDP protocol JSON files and generates a generated.rs containing:
- Types -- Structs, enums, and type aliases for each domain (
page::types::Frame,network::types::RequestId, ...) - Commands --
*Params/*Returnsstructs and typed async methods on domain clients - Events -- Event structs and typed registration methods
- CdpLibrary / CdpRegistrationLibrary -- Top-level accessors:
cdp.send().page.navigate(...),cdp.register().page.frame_navigated(...)
Comparison with Other Browser Automation Tools
| Selenium | Puppeteer | Playwright | cdp-use (Python) | cdp-use-rs | |
|---|---|---|---|---|---|
| Language | Java, Python, C#, JS, Ruby | Node.js | Node.js, Python, Java, .NET | Python | Rust |
| Protocol | WebDriver (W3C) | CDP | CDP + custom protocol | CDP (raw) | CDP (raw) |
| Browser support | Chrome, Firefox, Safari, Edge | Chrome, Firefox (experimental) | Chrome, Firefox, WebKit | Chrome (any CDP target) | Chrome (any CDP target) |
| Abstraction level | High-level | High-level | High-level | Low-level (1:1 CDP mapping) | Low-level (1:1 CDP mapping) |
| Type safety | Varies by language | TypeScript types | TypeScript types | TypedDict + Literal | Structs + enums + serde |
| Auto-wait / retries | Implicit/explicit waits | Built-in auto-wait | Built-in auto-wait | None (manual) | None (manual) |
| Selectors | CSS, XPath | CSS, XPath, aria | CSS, XPath, text, role, test-id | N/A (raw CDP) | N/A (raw CDP) |
| Code generation | N/A | N/A | codegen CLI |
Runtime script | Build-time build.rs |
| CDP domain coverage | Partial (abstracted away) | Partial (high-level API) | Partial (high-level API) | Full 53 domains | Full 53 domains |
| Use case | Cross-browser E2E testing | Chrome automation & scraping | Cross-browser E2E testing | Full CDP control, AI agent infra | Full CDP control, AI agent infra |
When to use cdp-use-rs
- You need direct access to all 53 CDP domains without abstraction layers getting in the way
- You're building AI browser agents or infrastructure that requires low-level protocol control
- You want compile-time type safety and zero-cost abstractions over the CDP protocol
- Performance matters -- Rust's async runtime and zero-overhead WebSocket handling
- You're integrating CDP into a larger Rust system (e.g., an agent framework, a proxy, or a custom browser controller)
When to use Playwright / Puppeteer / Selenium instead
- You need cross-browser support (Firefox, WebKit/Safari)
- You want high-level APIs with built-in auto-wait, selectors, and test assertions
- You're writing E2E tests for web applications
- You prefer a batteries-included approach with screenshots, tracing, and network interception out of the box
Comparison with Python cdp-use
| Python (cdp-use) | Rust (cdp-use-rs) | |
|---|---|---|
| Domains | 53 | 53 |
| Type system | TypedDict + Literal |
Structs + enums with serde |
| Optional fields | NotRequired[T] |
Option<T> + skip_serializing_if |
| Naming | camelCase (JS convention) | snake_case + #[serde(rename_all = "camelCase")] |
| Async runtime | asyncio | tokio |
| WebSocket | websockets | tokio-tungstenite |
| Code generation | Runtime script | Build-time build.rs |
| Event handlers | Sync or async callbacks | Async closures (spawned per event) |
Development
# Run the example (requires Chrome with --remote-debugging-port=9222)
Related
- cdp-use -- Original Python implementation
- Chrome DevTools Protocol -- Official protocol documentation
- browser-use -- Browser automation framework
License
MIT