Metadata-Version: 2.4
Name: fastcdp
Version: 0.0.3
Summary: Lightweight Chrome Debug Protocol (CDP) client for python
Author-email: Jeremy Howard <github@jhoward.fastmail.fm>
License: Apache-2.0
Project-URL: Repository, https://github.com/AnswerDotAI/fastcdp
Project-URL: Documentation, https://AnswerDotAI.github.io/fastcdp/
Keywords: nbdev
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: websockets
Requires-Dist: fastcore
Dynamic: license-file

# fastcdp


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

fastcdp provides an async Python client for the [Chrome DevTools
Protocol](https://chromedevtools.github.io/devtools-protocol/) (CDP)
over WebSocket. It auto-discovers Chrome’s debug port, loads the full
protocol schema from bundled JSON files, and exposes every CDP domain as
a Python attribute with auto-generated signatures and docstrings —
e.g. `await cdp.page.navigate(url=...)`.

It includes a
[`Page`](https://AnswerDotAI.github.io/fastcdp/core.html#page) class for
tab-scoped operations, event subscription via
`cdp.on()`/`cdp.wait_event()`, navigation helpers (`goto`,
`wait_for_selector`, `wait_for`), screenshot capture, and accessibility
tree access. A
[`cdp_search`](https://AnswerDotAI.github.io/fastcdp/core.html#cdp_search)
utility lets you search CDP commands by name or description. For use
inside [safepyrun](https://github.com/AnswerDotAI/safepyrun) sandboxes,
[`cdp_yolo()`](https://AnswerDotAI.github.io/fastcdp/core.html#cdp_yolo)
registers all CDP classes.

## Installation

Install latest from [pypi](https://pypi.org/project/fastcdp/)

``` sh
$ pip install fastcdp
```

## How to use

``` python
from fastcdp import *
```

Chrome 146+ has built-in remote debugging support. Navigate to
`chrome://inspect/#remote-debugging` and enable “Allow remote debugging
for this browser instance”:

![image.png](index_files/figure-commonmark/65d1f5d3-1-5d9f96ff-4344-43ee-bb5d-00d465cf1f79.png)

Connect to Chrome (which will pop up a permissions window):

``` python
cdp = await CDP.connect()
```

Every CDP domain is available as an attribute with auto-generated
signatures. You can search for commands with
[`cdp_search`](https://AnswerDotAI.github.io/fastcdp/core.html#cdp_search):

``` python
cdp_search('screenshot')
```

    "Emulation.setVisibleSize: Resizes the frame/viewport of the page. Note that this does not affect the frame's container\n(e.g. browser window). Can \nHeadlessExperimental.beginFrame: Sends a BeginFrame to the target and returns when the frame was completed. Optionally captures a\nscreenshot from the res\n  evt Overlay.screenshotRequested: Fired when user asks to capture screenshot of some area on the page.\nPage.captureScreenshot: Capture page screenshot."

List open pages and attach to one:

``` python
ps = await cdp.pages
pg = ps[0]
pg['title']
```

    'l:SolveIT - scratch◀️chats'

``` python
tid = pg['targetId']
sid = await cdp.attach(tid)
await cdp.eval('document.title', sid)
```

    'l:SolveIT - scratch◀️chats'

The [`Page`](https://AnswerDotAI.github.io/fastcdp/core.html#page) class
wraps a tab with its own session, so you don’t need to pass `sid`
everywhere:

``` python
page = await cdp.new_page()
await page.goto('https://httpbin.org/forms/post')
```

You can `wait_for` any js expression to be truthy, and have it returned:

``` python
await page.wait_for('document.title')
```

    '4. httpbin.org/forms/post'

Take a screenshot of the page:

``` python
img = await page.screenshot()
```

Clean up when done:

``` python
await page.close()
await cdp.close()
```

See [`CDP`](https://AnswerDotAI.github.io/fastcdp/core.html#cdp) docs
for full details.
