cdp-driver
Chrome DevTools Protocol (CDP) client in Rust. WebSocket-based browser automation for AI agents, web scraping, and testing.
Quick Start
Start Chrome with remote debugging:
# or headless
Add to Cargo.toml:
[]
= "0.2"
# optional: synchronous blocking API
= { = "0.2", = ["blocking"] }
Usage
Async (default)
use ;
async
Blocking (feature = "blocking")
No async runtime needed - each client owns its own tokio runtime internally.
use BrowserAgent;
use ;
JSON actions (LLM tool calls)
agent.execute_json.await;
agent.execute_json.await;
agent.execute_json.await;
agent.execute_json.await;
Action builder
use ActionBuilder;
let actions = new
.navigate
.wait
.fill
.press_key
.wait
.screenshot
.build;
let results = agent.execute_many.await;
Low-level client
use ;
async
Events
let mut rx = client.subscribe_events;
client.navigate.await?;
while let Ok = rx.recv.await
// wait for a specific event with timeout
let metrics = client.wait_for_event.await?;
Console capture
let mut console = agent.capture_console;
agent.execute.await;
while let Ok = console.recv.await
Network interception
client.enable_domain.await?;
client.intercept_requests.await?;
let mut rx = client.subscribe_events;
client.navigate_and_wait.await?;
while let Ok = rx.recv.await
PDF export
client.navigate_and_wait.await?;
client.print_to_pdf.await?;
Emulation
client.set_user_agent.await?;
client.set_geolocation.await?;
client.set_offline.await?;
Cluster (puppeteer-cluster style)
Pre-creates a pool of browser tabs and distributes tasks across them with retries. Workers are reused between tasks - no create/close overhead per task.
use ;
use Config;
let cluster = new.await?;
let results = cluster.run.await;
cluster.close.await;
ClusterConfig fields:
| Field | Default | Description |
|---|---|---|
concurrency |
5 |
number of worker tabs |
retries |
2 |
retries per task before failure |
monitor |
false |
print per-task timing |
Config
Config::default() sets:
| Field | Default |
|---|---|
host |
localhost |
port |
9222 |
viewport_width |
1920 |
viewport_height |
1200 |
screenshots_dir |
screenshots |
Actions
| Action | Parameters |
|---|---|
navigate |
url |
go_back / go_forward / reload |
- |
click |
selector or x, y |
type |
text, selector? |
fill |
selector, value |
submit |
selector? |
press_key |
key |
get_title / get_url / get_text |
- |
get_content |
selector? |
get_links / get_attributes / exists |
selector |
screenshot |
path? |
evaluate |
expression |
wait |
ms |
wait_for_selector |
selector, timeout_ms? |
scroll |
x, y |
set_viewport |
width, height, mobile? |
get_metrics |
- |
Debug Logging
# debug CDP send/recv/events
RUST_LOG=cdp_driver=debug
# synchronous log output (easier to correlate with code flow)
RUST_LOG=cdp_driver=debug RUST_LOG_SYNC=1
# everything including tokio/reqwest internals
RUST_LOG=debug
Examples
Node / Deno / Bun
The same engine ships as an npm package via napi-rs bindings
in crates/cdp-protocol-node. Native Rust does the CDP
work; JS gets Promises and TypeScript types. Works in Node, Bun, and Deno
(npm: specifier).
import from 'cdp-driver'
const agent = await
await agent.
console.log
await agent.
CdpClient (low-level), BrowserAgent (actions), and Cluster (worker pool)
are exposed to JS. CdpClient mirrors the Rust client/network/page surface;
Cluster is a purpose-built batch pool (not a port of the generic Rust
Cluster). See crates/cdp-protocol-node.
Project Structure
src/
├── lib.rs # public exports
├── client.rs # CDP WebSocket client, event system
├── agent.rs # high-level agent, BrowserAction enum, ActionBuilder
├── cluster.rs # worker pool (Cluster, ClusterConfig)
├── blocking.rs # synchronous wrappers (feature = "blocking")
├── network.rs # network methods (cookies, headers, interception)
├── page.rs # page/emulation/DOM methods (PDF, user agent, geolocation)
├── config.rs # Config struct
├── types.rs # protocol types
└── error.rs # CdpError
examples/
├── basic.rs # low-level usage
├── agent.rs # agent + JSON dispatch + builder
├── industrial.rs # parallel scraping with JoinSet
├── cluster.rs # worker pool with retries
└── common/
└── logging.rs # shared tracing init