Metadata-Version: 2.4
Name: cdipy
Version: 0.6.0
Summary: An async Python interface for Chrome Devtools
Author-email: Pilate <pilate@pilate.es>
License-Expression: MIT
Project-URL: Homepage, https://github.com/pilate/cdipy
Project-URL: Issues, https://github.com/pilate/cdipy/issues
Keywords: chrome,devtools,cdp,headless,automation
Classifier: Development Status :: 5 - Production/Stable
Classifier: Framework :: AsyncIO
Classifier: Intended Audience :: Developers
Classifier: Operating System :: POSIX
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Internet :: WWW/HTTP :: Browsers
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE.md
Requires-Dist: msgspec<0.21.0,>=0.18
Requires-Dist: websockets<17,>=14.1
Dynamic: license-file

# cdipy
Chrome Devtools Interface that instruments Chrome via the [devtools protocol](https://github.com/ChromeDevTools/devtools-protocol).

Meant to serve as a pythonic version of [chrome-remote-interface](https://github.com/cyrus-and/chrome-remote-interface).

> **Note:** cdipy requires CDP flat mode. Always pass `flatten=True` when calling `Target.attachToTarget`, otherwise target sessions will not receive messages.

### Example Usage
```python

import asyncio
import base64
import logging
import sys

from cdipy import ChromeDevTools
from cdipy import ChromeDevToolsTarget
from cdipy import ChromeRunner


LOGGER = logging.getLogger("cdipy.scripts.screenshot")
FILENAME = "screenshot.png"


async def async_main(url):
    # Start Chrome
    chrome = ChromeRunner()
    await chrome.launch()

    # Connect to devtools websocket
    cdi = ChromeDevTools(chrome.websocket_uri)
    await cdi.connect()

    # Create a new target and attach to it
    target = await cdi.Target.createTarget(url="about:blank")
    session = await cdi.Target.attachToTarget(targetId=target["targetId"], flatten=True)

    # Create a ChromeDevToolsTarget class to handle target messages
    cdit = ChromeDevToolsTarget(cdi, session["sessionId"])

    # Enable 'Page' events
    await cdit.Page.enable()

    # Navigate to URL
    LOGGER.info("Navigating to %s", url)
    await cdit.Page.navigate(url=url)

    # Wait for the Page.loadEventFired event
    # This may not ever fire on some pages, so it's good to set a limit
    try:
        await cdit.wait_for("Page.loadEventFired", 10)
    except asyncio.TimeoutError:
        print("Loaded event never fired!")

    # Take a screenshot
    screenshot_response = await cdit.Page.captureScreenshot(format="png")
    screenshot_bytes = base64.b64decode(screenshot_response["data"])

    with open(FILENAME, "w+b") as fileobj:
        fileobj.write(screenshot_bytes)

    LOGGER.info("wrote %s", FILENAME)

    # Disconnect from devtools; chrome is killed when the runner is collected
    await cdi.close()


def main():
    logging.basicConfig(level=logging.INFO)

    asyncio.run(async_main(sys.argv[1]))


if __name__ == "__main__":
    main()


```

### Skipping Parameter Validation

By default, cdipy validates the parameters you pass to CDP commands against the protocol definition. To skip this validation, set the `CDIPY_SKIP_VALIDATION` environment variable:

```bash
export CDIPY_SKIP_VALIDATION=1
```

This passes your keyword arguments directly to the protocol without checking them against the command's parameter list. Domains and commands are resolved dynamically by name, so the protocol definition is never downloaded at all. Useful if the cached protocol definition is out of date, or to avoid the download entirely.
