Skip to main content

An async Python interface for Chrome Devtools

Project description

cdipy

Chrome Devtools Interface that instruments Chrome via the devtools protocol.

Meant to serve as a pythonic version of chrome-remote-interface.

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"])

    # 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)


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

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


if __name__ == "__main__":
    main()

Project details


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.4.4.tar.gz (7.4 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.4.4-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cdipy-0.4.4.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cdipy-0.4.4.tar.gz
Algorithm Hash digest
SHA256 ed4c1249a479ba4179e52e84b89bf09ea939aa637191bf52c3571d5220d66c84
MD5 067b4c7552c103c7703f4848f8da8852
BLAKE2b-256 81dd436a2117527bce2d35250e9c339c3409889bed4c3b2f3fa698588493a2d2

See more details on using hashes here.

Provenance

The following attestation bundles were made for cdipy-0.4.4.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.4.4-py3-none-any.whl.

File metadata

  • Download URL: cdipy-0.4.4-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cdipy-0.4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 cfa468748be2c1a7a35d218a6cccea7f44baf5dc43a0fb5bb3ea36ac45645c2b
MD5 b6a0b12526ea9a39e32643a430df6114
BLAKE2b-256 6542def68d24ca03678b89c1dab2042d5691e8ca64e3bb1cc87efa98d988da30

See more details on using hashes here.

Provenance

The following attestation bundles were made for cdipy-0.4.4-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.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page