cdpify
Fully typed, Pythonic access to the Chrome DevTools Protocol.
The Chrome DevTools Protocol (CDP) is a JSON-over-WebSocket API with hundreds
of commands and events. cdpify generates a typed Python API from the official
specification: every command is a method, every parameter a keyword argument,
every result and event a dataclass, and your editor knows all of them.
Raw CDP: hand-built JSON, string keys, untyped results.
await ws.send(json.dumps({
"id": 1,
"method": "Runtime.evaluate",
"params": {"expression": "document.title", "returnByValue": True},
}))
title = json.loads(await ws.recv())["result"]["result"]["value"]
cdpify: one typed, autocompleted call.
result = await client.runtime.evaluate(
expression="document.title",
return_by_value=True,
)
title = result.result.value
Why cdpify?
- Typed end to end. Arguments, results, events and nested protocol types are generated dataclasses, enums and aliases. Autocomplete, hover docs and Pyright/mypy checks work out of the box.
- Pythonic.
snake_casenames, keyword arguments,async with,async for,StrEnums and regular exceptions.cdpifyconverts to and from thecamelCasewire format for you. - Complete. All 59 CDP domains, regenerated weekly from upstream Chrome. The spec's descriptions, units and deprecation markers land in the docstrings.
- Lean. No required dependencies; the WebSocket transport and
orjsonspeedups are optional extras.
Installation
pip install "cdpify[websocket]"
Requires Python 3.12+. Add the orjson extra for faster JSON
(pip install "cdpify[websocket,orjson]").
Upgrading from 0.5.x? See the 0.6.0 migration guide.
Quick start
Start Chrome with remote debugging enabled:
chrome --remote-debugging-port=9222 --user-data-dir=/tmp/cdpify-profile
Then connect to a tab and drive it:
import asyncio
import json
from urllib.request import urlopen
from cdpify import CdpClient
def page_url() -> str:
with urlopen("http://localhost:9222/json") as response:
targets = json.load(response)
return next(t["webSocketDebuggerUrl"] for t in targets if t["type"] == "page")
async def main() -> None:
async with CdpClient(page_url()) as client:
await client.page.navigate(url="https://example.com")
result = await client.runtime.evaluate(
expression="document.title",
return_by_value=True,
)
print(result.result.value)
asyncio.run(main())
Core API at a glance
Almost everything you do with cdpify uses these few building blocks:
| You want to… | API |
|---|---|
| Connect | async with CdpClient(ws_url) as client: |
| Send a command | await client.<domain>.<command>(**params), e.g. client.page.navigate(url=...) |
| Wait for one event | async with client.listen(Event) as sub: … await sub.next(timeout_seconds=10) |
| Stream events | async for event in client.listen(Event): |
| Control another tab | tab = await client.attach(target_id), then tab.page, tab.listen(...) |
| Limit how long a call may take | async with asyncio.timeout(5): |
| Call something not in the spec | await client.execute("Domain.method", {...}) |
Commands
Domains are properties, commands are methods, results are dataclasses:
document = await client.dom.get_document()
heading = await client.dom.query_selector(node_id=document.root.node_id, selector="h1")
html = await client.dom.get_outer_html(node_id=heading.node_id)
print(html.outer_html)
Events
Subscribe with a generated event class and iterate. Subscriptions buffer from the moment they are created, so subscribe first and then trigger the event:
from cdpify.domains.page import LoadEventFiredEvent
await client.page.enable()
async with client.listen(LoadEventFiredEvent) as loaded:
await client.page.navigate(url="https://example.com")
await loaded.next(timeout_seconds=10)
from cdpify.domains.network import ResponseReceivedEvent
await client.network.enable()
async for event in client.listen(ResponseReceivedEvent):
print(event.response.status, event.response.url)
Sessions
Connect to the browser once and drive many targets concurrently:
target = await client.target.create_target(url="https://example.org")
tab = await client.attach(target.target_id)
await tab.page.enable()
shot = await tab.page.capture_screenshot(format="png")
Errors
Failures are ordinary exceptions:
from cdpify import CDPCommandError
try:
await client.dom.query_selector(node_id=-1, selector="h1")
except CDPCommandError as error:
print(error.method, error.code, error.message)
Documentation
The docs are a learning path: each level builds on the previous one.
| Level | Guide | You learn |
|---|---|---|
| 1 | First steps | Connecting, commands, naming, enums, types |
| 2 | Events | Subscriptions, timeouts, scopes, buffering |
| 3 | Multiple targets | Attaching to tabs, sessions, concurrency |
| 4 | Timeouts and errors | Deadlines, error types, decoding, UNSET |
| 5 | Configuration and transports | All client options, custom transports |
| ★ | Cookbook | Complete recipes, from screenshots to request interception |
See also the migration guide, development guide, runnable examples and the changelog.
Resources
The code-generation approach was inspired by
cdp-use.
Metadata
Release files for cdpify 0.6.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cdpify-0.6.0.tar.gz | 405.3 kB | Without provenance | Details for cdpify-0.6.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cdpify-0.6.0-py3-none-any.whl | Python 3 | none | any | Details for cdpify-0.6.0-py3-none-any.whl |
Total release size: 934.7 kB
Release files / cdpify-0.6.0.tar.gz
| Download URL | cdpify-0.6.0.tar.gz |
|---|---|
| Size | 405.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dfed7655c14733c0631fadb99f64ce8f838926b5e3759960221d88ce73cc8641
|
|
BLAKE2b-256 checksum How to use checksums |
7977b2f74a2476c81d062ec65ab0957ba0f872144d373882e3d6a3f475bcb936
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.2
|
Release files / cdpify-0.6.0-py3-none-any.whl
| Download URL | cdpify-0.6.0-py3-none-any.whl |
|---|---|
| Size | 529.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d43df3ad8b680737e76121bb7c3927f4e2b042132ffd4ed954d8194cead6ae10
|
|
BLAKE2b-256 checksum How to use checksums |
e61c215540009b08a2be159a121328c0e0addeeb027ec2cba1511f6e25fad282
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.2
|
Security for cdpify 0.6.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.