cdp-driver
Chrome DevTools Protocol (CDP) client in Rust. WebSocket-based browser automation for AI agents, web scraping, and testing.
Covers the whole protocol: all 677 commands, 232 events and 611 types are generated from the published schema, alongside a small hand-written API for the things you reach for constantly.
Quick Start
Start Chrome with remote debugging:
# or headless
Add to Cargo.toml:
[]
= "0.5"
# optional: synchronous blocking API
= { = "0.5", = ["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
Typed commands and events
Every command is a struct that knows its own method name and result type, so the
three cannot disagree. Generated from the protocol schema into
cdp_driver::protocol, one module per domain.
use ;
use EvaluateParams;
client.send.await?;
let navigated = client.send.await?;
assert!;
let loaded = client..await?;
println!;
let evaluated = client.send.await?;
Schema enums keep an Unrecognized(String) variant, because Chrome ships values
ahead of the published protocol and a payload should not fail to decode just
because it is newer than this crate.
Any command by name
call and call_raw send anything, for exploring a response or reaching a
command with no typed struct to hand. cdp_driver::methods holds a constant per
command so a typo is a compile error rather than a runtime one.
use target;
use CreateTargetReturns;
use json;
// deserialized into a type you name
let created: CreateTargetReturns = client
.call
.await?;
// or raw, to index into while you are still learning a response
let targets = client.call_raw.await?;
Sessions
Attaching reaches a target other than the one you connected to (another tab, an out-of-process iframe, a worker) over the same socket, using CDP's flat session mode.
let session = client.attach_to_target.await?;
session.send.await?;
session.send.await?;
session.detach.await?;
subscribe_session_events() yields SessionEvent { session_id, method, params },
where session_id is None for the target you connected to directly. The older
subscribe_events() keeps its exact meaning and only ever sees those untagged
events, so an attached tab's activity cannot wake a waiter watching another tab.
Errors
CdpError::Browser { code, message, data } is what Chrome rejected, carrying
CDP's own numeric code (-32601 for an unknown method, -32602 for bad
parameters). Branch on code rather than on the wording of message.
CdpError::Protocol(String) is reserved for what this crate itself could not
make sense of.
Two calls report failure rather than hiding it: navigate fails when Chrome
answers with an errorText such as net::ERR_NAME_NOT_RESOLVED, and eval
fails when the expression throws. Use evaluate when you want an exception back
as data instead of as an error.
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 # the session: socket, command channel, frame routing, events
├── session.rs # CdpSession, attaching to other targets
├── typed.rs # Command and Event traits, send/wait_for/decode
├── protocol/ # generated: 677 commands, 232 events, 611 types
├── methods/ # generated: a name constant per command
├── discovery.rs # Chrome's HTTP /json endpoints
├── page.rs # navigation, emulation, PDF, geolocation
├── network.rs # cookies, headers, interception
├── dom.rs # document tree
├── runtime.rs # JavaScript evaluation
├── screenshot.rs # PNG capture
├── agent.rs # BrowserAgent: runs actions, reports outcomes
├── action.rs # BrowserAction, the action vocabulary
├── action_parse.rs # BrowserAction from LLM tool-call JSON
├── action_builder.rs # ActionBuilder
├── keys.rs # keyboard names for Input.dispatchKeyEvent
├── cluster.rs # worker pool (Cluster, ClusterConfig)
├── blocking.rs # synchronous wrappers (feature = "blocking")
├── config.rs # Config struct
├── types.rs # hand-written types for the curated API
└── 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
├── raw.rs # call/call_raw into domains with no wrapper
└── common/
└── logging.rs # shared tracing init
Generated code comes from the schemas vendored in protocol/ at the repository
root. just generate-methods regenerates it; just update-protocol refreshes
the schemas from upstream first.