Skip to content

ConnectionHandler

Gerencia a conexão WebSocket com o navegador e roteia os comandos e eventos do CDP. Saiba mais em Conexões remotas.

pydoll.connection.connection_handler.ConnectionHandler

ConnectionHandler(connection_port=None, page_id=None, ws_address_resolver=get_browser_ws_address, ws_connector=connect, ws_address=None, max_message_size=DEFAULT_MAX_MESSAGE_SIZE)

WebSocket connection manager for Chrome DevTools Protocol endpoints.

Handles connection lifecycle, command execution, and event subscription for both browser-level and page-level CDP endpoints.

Initialize connection handler.

PARAMETER DESCRIPTION
connection_port

Browser's debugging server port.

TYPE: int | None DEFAULT: None

page_id

Target page ID. If None, connects to browser-level endpoint.

TYPE: str | None DEFAULT: None

ws_address_resolver

Function to resolve WebSocket URL from port.

TYPE: Callable[[int], Coroutine[Any, Any, str]] DEFAULT: get_browser_ws_address

ws_connector

WebSocket connection factory (mainly for testing).

TYPE: type[connect] DEFAULT: connect

ws_address

WebSocket address. It has priority over connection_port and page_id.

TYPE: str | None DEFAULT: None

max_message_size

Largest message, in bytes, the connection accepts. A larger one closes the connection. None removes the limit.

TYPE: int | None DEFAULT: DEFAULT_MAX_MESSAGE_SIZE

max_message_size property

max_message_size

Largest message, in bytes, this connection accepts (None means no limit).

target_crashed property

target_crashed

Whether the target's renderer crashed and has not been reloaded since.

Set by Inspector.targetCrashed and cleared by Inspector.targetReloadedAfterCrash, which Chrome sends once a navigation or a reload starts a new renderer.

network_logs property

network_logs

Access captured network request and response logs.

dialog property

dialog

Access currently active JavaScript dialog information.

ping async

ping(timeout=_PING_TIMEOUT)

Test if the WebSocket connection is open and the browser answers on it.

Opens a connection when there is none, sends a ping frame and waits for the pong. A stopped or frozen browser process keeps its socket open, so sending the ping succeeds; the pong shows the connection is still read. It does not prove that commands are processed: on the Linux CI runners a browser stopped with SIGSTOP was seen answering pings, which is why the Browser's own liveness checks send a CDP command instead.

PARAMETER DESCRIPTION
timeout

Seconds to wait for the connection and the pong together.

TYPE: float DEFAULT: _PING_TIMEOUT

RETURNS DESCRIPTION
bool

True if the pong arrived within timeout, False otherwise.

wait_disconnected async

wait_disconnected()

Wait until the current connection stops receiving messages.

A caller waiting for an event, rather than for a command's answer, is not failed when the connection drops, because no pending command stands for it. Racing its wait against this lets it stop as soon as the connection that would have delivered the event is gone.

RETURNS DESCRIPTION
WebSocketConnectionClosed

The error describing why the connection closed, the same one its

WebSocketConnectionClosed

pending commands received. Returned at once when there is no live

WebSocketConnectionClosed

connection.

execute_command async

execute_command(command, timeout=60)

Send CDP command and await response.

PARAMETER DESCRIPTION
command

CDP command to send.

TYPE: Command[T_CommandParams, T_CommandResponse]

timeout

Maximum seconds to wait for response.

TYPE: int DEFAULT: 60

RETURNS DESCRIPTION
T_CommandResponse

Parsed response object matching command's expected type.

RAISES DESCRIPTION
CommandFailed

If the browser answers with a CDP error instead of a result.

InvalidResponse

If the response carries neither a result nor an error.

CommandExecutionTimeout

If browser doesn't respond within timeout.

WebSocketConnectionClosed

If connection closes during execution.

ConnectionFailed

If the connection cannot be (re)established.

TargetCrashed

If the target's renderer crashed, for a command that needs it, either before sending or while waiting for the answer.

execute_command_nowait async

execute_command_nowait(command)

Send a CDP command and return as soon as it is on the wire.

The answer is still consumed when it arrives, so the pending table stays clean, and a CDP error is logged instead of raised. Meant for streams of input events whose order the socket already guarantees: awaiting each answer would add a network round trip between consecutive mouse moves, which no real input device has.

PARAMETER DESCRIPTION
command

CDP command to send.

TYPE: Command[T_CommandParams, T_CommandResponse]

RAISES DESCRIPTION
ConnectionFailed

If the connection cannot be (re)established.

WebSocketConnectionClosed

If the connection closes before the command is sent.

TargetCrashed

If the target's renderer crashed and the command needs it.

execute_commands async

execute_commands(commands, timeout=60)

Send several commands back to back and wait for all of their answers.

The browser handles the commands of one session in the order they arrive, so sending a sequence without waiting for each answer keeps the ordering (keydown before keyup, for example) while paying one round trip for the whole batch instead of one per command.

PARAMETER DESCRIPTION
commands

Commands to send, in order.

TYPE: Sequence[Command[T_CommandParams, T_CommandResponse]]

timeout

Seconds to wait for the last answer.

TYPE: int DEFAULT: 60

RETURNS DESCRIPTION
list[T_CommandResponse]

The responses, in the same order as commands.

RAISES DESCRIPTION
CommandFailed

If the browser rejects any command in the batch.

CommandExecutionTimeout

If the answers do not all arrive in time.

WebSocketConnectionClosed

If the connection closes meanwhile.

ConnectionFailed

If the connection cannot be (re)established.

TargetCrashed

If the target's renderer crashed and any command needs it.

register_callback async

register_callback(event_name, callback, temporary=False)

Register event listener for CDP events.

PARAMETER DESCRIPTION
event_name

CDP event name (e.g., 'Page.loadEventFired').

TYPE: str

callback

Async function called when event occurs.

TYPE: Callable[[dict], Awaitable[None]]

temporary

If True, callback removed after first trigger.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
int

Callback ID for later removal.

Note

Corresponding CDP domain must be enabled before events fire.

remove_callback async

remove_callback(callback_id)

Remove registered event callback by ID.

clear_callbacks async

clear_callbacks()

Remove all registered event callbacks.

close async

close()

Close WebSocket connection and release resources.

The domains this session had enabled are forgotten too, so a later command opens a connection with every domain off. A connection being opened meanwhile is torn down once it is up, so none is left open.