Skip to content

CDP

The CDP module provides access to Chrome DevTools Protocol commands. For full usage examples, see CDP.

Chrome/Edge only

CDP commands are only supported by Chromium-based browsers.

Methods

send_command

Send a raw CDP command:

result = await client.cdp.send_command(
    method="Performance.getMetrics",
    params={},
)
# result.result contains the CDP response

get_session

Get the CDP session ID for a specific browsing context:

session = await client.cdp.get_session(context="context-id-1")
# session.session = "cdp-session-id"

Reference

CDPModule

Module for sending CDP commands directly (Chrome-only).

Allows access to Chrome DevTools Protocol APIs that are not yet covered by WebDriver BiDi.

Example

cdp_session = await client.cdp.get_session() result = await client.cdp.send_command("Page.reload", {})

Source code in bidiwave/modules/cdp.py
class CDPModule:
    """Module for sending CDP commands directly (Chrome-only).

    Allows access to Chrome DevTools Protocol APIs that are not
    yet covered by WebDriver BiDi.

    Example:
        cdp_session = await client.cdp.get_session()
        result = await client.cdp.send_command("Page.reload", {})
    """

    def __init__(self, connection: Connection) -> None:
        self._connection = connection

    async def get_session(self) -> str | None:
        """Gets the current CDP session ID.

        Returns:
            CDP session ID or None if no active session.
        """
        result = await self._connection.send_command(
            BROWSER_CDP_GET_SESSION, {}
        )
        parsed = CDPGetSessionResult.model_validate(result)
        return parsed.session

    async def send_command(
        self,
        cmd: str,
        params: dict[str, Any] | None = None,
    ) -> dict[str, Any]:
        """Sends a CDP command directly to the browser.

        Args:
            cmd: CDP command name (e.g. "Page.reload", "Network.enable").
            params: Command parameters.

        Returns:
            CDP command result as dict.
        """
        send_params: dict[str, Any] = {"cmd": cmd}
        if params is not None:
            send_params["params"] = params
        return await self._connection.send_command(
            BROWSER_CDP_SEND_COMMAND, send_params
        )

get_session async

get_session() -> str | None

Gets the current CDP session ID.

Returns:

Type Description
str | None

CDP session ID or None if no active session.

Source code in bidiwave/modules/cdp.py
async def get_session(self) -> str | None:
    """Gets the current CDP session ID.

    Returns:
        CDP session ID or None if no active session.
    """
    result = await self._connection.send_command(
        BROWSER_CDP_GET_SESSION, {}
    )
    parsed = CDPGetSessionResult.model_validate(result)
    return parsed.session

send_command async

send_command(cmd: str, params: dict[str, Any] | None = None) -> dict[str, Any]

Sends a CDP command directly to the browser.

Parameters:

Name Type Description Default
cmd str

CDP command name (e.g. "Page.reload", "Network.enable").

required
params dict[str, Any] | None

Command parameters.

None

Returns:

Type Description
dict[str, Any]

CDP command result as dict.

Source code in bidiwave/modules/cdp.py
async def send_command(
    self,
    cmd: str,
    params: dict[str, Any] | None = None,
) -> dict[str, Any]:
    """Sends a CDP command directly to the browser.

    Args:
        cmd: CDP command name (e.g. "Page.reload", "Network.enable").
        params: Command parameters.

    Returns:
        CDP command result as dict.
    """
    send_params: dict[str, Any] = {"cmd": cmd}
    if params is not None:
        send_params["params"] = params
    return await self._connection.send_command(
        BROWSER_CDP_SEND_COMMAND, send_params
    )