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.
Note: cdipy requires CDP flat mode. Always pass
flatten=Truewhen callingTarget.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)
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 signature. Useful if the cached protocol definition is out of date or you want to avoid the overhead of signature binding.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cdipy-0.5.0.tar.gz.
File metadata
- Download URL: cdipy-0.5.0.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
203f962ece604d9cd02146eaf0d951904fbe85481ced7b8f9a6b6f0c74b1a121
|
|
| MD5 |
24f32ae1391bf877b297f21d3b76b71b
|
|
| BLAKE2b-256 |
c527149a0dca3990652ff1770cbc6c889e2b63af60b9d3a2b675d235b9c952fa
|
Provenance
The following attestation bundles were made for cdipy-0.5.0.tar.gz:
Publisher:
publish-to-pypi.yml on pilate/cdipy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cdipy-0.5.0.tar.gz -
Subject digest:
203f962ece604d9cd02146eaf0d951904fbe85481ced7b8f9a6b6f0c74b1a121 - Sigstore transparency entry: 1046387252
- Sigstore integration time:
-
Permalink:
pilate/cdipy@0aed96e249a575229c3db5b6b404514c4efbd304 -
Branch / Tag:
refs/tags/0.5.0 - Owner: https://github.com/pilate
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@0aed96e249a575229c3db5b6b404514c4efbd304 -
Trigger Event:
push
-
Statement type:
File details
Details for the file cdipy-0.5.0-py3-none-any.whl.
File metadata
- Download URL: cdipy-0.5.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f9359eea629e1d34dcefa26189dcdeb59283eeeb4d4cd89e38157ab7254b5ef
|
|
| MD5 |
86029c9b903163208826ed59d503ce01
|
|
| BLAKE2b-256 |
e23eeeedf51509e412a00c601c5230ac1ffb23cfb82d7fe556deb012f279a04a
|
Provenance
The following attestation bundles were made for cdipy-0.5.0-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on pilate/cdipy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cdipy-0.5.0-py3-none-any.whl -
Subject digest:
0f9359eea629e1d34dcefa26189dcdeb59283eeeb4d4cd89e38157ab7254b5ef - Sigstore transparency entry: 1046387308
- Sigstore integration time:
-
Permalink:
pilate/cdipy@0aed96e249a575229c3db5b6b404514c4efbd304 -
Branch / Tag:
refs/tags/0.5.0 - Owner: https://github.com/pilate
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@0aed96e249a575229c3db5b6b404514c4efbd304 -
Trigger Event:
push
-
Statement type: