Skip to main content

Low-level client for interacting with Chrome Devtools Protocol (CDP) for Python.

Project description

cidipi

Low-level client for interacting with Chrome Devtools Protocol (CDP) for Python.

[!WARNING] This library is in alpha. Use it at your own risk.

Installation

pip install cidipi

Usage

import asyncio
import logging
from typing import cast

from cidipi.client import Browser
from cidipi.models import EventResponse, Payload, Tab
from cidipi.protocols import commands, events

logging.basicConfig(
    level=logging.DEBUG, format="[%(levelname)s] [%(name)s] %(message)s"
)
logging.getLogger("websockets.client").disabled = True


async def navigate(tab: Tab, *, url: str, wait_until: str):
    async def wait_for(data: Payload, result: asyncio.Future):
        if data.kind == "result":
            return

        cdp_event_klass = events.Page.lifecycleEvent
        cdp_event_name = cdp_event_klass.get_method_name()
        resp_data = cast(EventResponse, data.data)
        if resp_data["method"] == cdp_event_name:
            obj = cdp_event_klass(**resp_data["params"])
            if obj.name == wait_until:
                result.set_result(None)

    return await tab.execute(
        commands.Page.navigate(url=url), callback=wait_for, timeout=30
    )


async def main():
    async with Browser(headless=False, remote_port=0) as browser:

        async def run(url: str):
            async with browser.new_tab() as tab:
                await tab.execute(commands.Page.enable())
                await tab.execute(commands.Page.setLifecycleEventsEnabled(enabled=True))
                await navigate(tab, url=url, wait_until="load")

        await run("https://www.google.com")


if __name__ == "__main__":
    asyncio.run(main())

See the examples/ directory to get started.

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

cidipi-0.2.0.tar.gz (132.6 kB view details)

Uploaded Source

Built Distribution

cidipi-0.2.0-py3-none-any.whl (135.7 kB view details)

Uploaded Python 3

File details

Details for the file cidipi-0.2.0.tar.gz.

File metadata

  • Download URL: cidipi-0.2.0.tar.gz
  • Upload date:
  • Size: 132.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for cidipi-0.2.0.tar.gz
Algorithm Hash digest
SHA256 98ed956783517531b3c6ca768b1d4d245e2834e44c00447a526f9bcdc3a7d41e
MD5 18667b88214100ddc080064c447279be
BLAKE2b-256 173641dd2a15c65e464c63bddb34df89f01f7ea0d10de9da5010f66441ae11c8

See more details on using hashes here.

File details

Details for the file cidipi-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: cidipi-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 135.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for cidipi-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 723d3c77c6626412f17b7a1921d5466e5f937e42d65ebc74f978316ee11f1a4e
MD5 24de767c07893adbf2f79041c8169ddb
BLAKE2b-256 e1ffdda08aedfcb5eab11bfe3f2cbbae78e200bdb1e9f0f46a30a70ff4e3d9a9

See more details on using hashes here.

Supported by

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