Skip to main content

cdipy

Chrome Devtools Interface that instruments Chrome via the devtools protocol.

Meant to serve as a pythonic version of 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

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:

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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cdipy-0.6.0.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cdipy-0.6.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file cdipy-0.6.0.tar.gz.

File metadata

  • Download URL: cdipy-0.6.0.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for cdipy-0.6.0.tar.gz
Algorithm Hash digest
SHA256 016324459fe95b4db4f5037f3f7e983bf68de1eea6c926177e87659aef7b824d
MD5 1f6c0e31959fdef4ef6441d3893148b9
BLAKE2b-256 f3545fbbac0c116ee34fa252bd46c3c06ad71bce32b112ef567c92069163fd5b

See more details on using hashes here.

Provenance

The following attestation bundles were made for cdipy-0.6.0.tar.gz:

Publisher: publish-to-pypi.yml on pilate/cdipy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cdipy-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: cdipy-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for cdipy-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f90d0830af2d54d37437bb06be81c8d3e7d2115fbb19433c3eefaece0fda79e9
MD5 b1a1ecea48b71f4dbf245b79124d566b
BLAKE2b-256 48442945a29b0bb9a4ec96de3649293123f462d8fe66029b1a489118f3484a83

See more details on using hashes here.

Provenance

The following attestation bundles were made for cdipy-0.6.0-py3-none-any.whl:

Publisher: publish-to-pypi.yml on pilate/cdipy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.6.0 This release

2 files

0.5.0

2 files

0.4.4

2 files

0.4.3

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page