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:
|
page_id
|
Target page ID. If None, connects to browser-level endpoint.
TYPE:
|
ws_address_resolver
|
Function to resolve WebSocket URL from port.
TYPE:
|
ws_connector
|
WebSocket connection factory (mainly for testing).
TYPE:
|
ws_address
|
WebSocket address. It has priority over connection_port and page_id.
TYPE:
|
max_message_size
|
Largest message, in bytes, the connection accepts. A larger one closes the connection. None removes the limit.
TYPE:
|
max_message_size
property
Largest message, in bytes, this connection accepts (None means no limit).
target_crashed
property
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.
ping
async
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:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the pong arrived within |
wait_disconnected
async
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
Send CDP command and await response.
| PARAMETER | DESCRIPTION |
|---|---|
command
|
CDP command to send.
TYPE:
|
timeout
|
Maximum seconds to wait for response.
TYPE:
|
| 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
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:
|
| 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
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:
|
timeout
|
Seconds to wait for the last answer.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[T_CommandResponse]
|
The responses, in the same order as |
| 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 event listener for CDP events.
| PARAMETER | DESCRIPTION |
|---|---|
event_name
|
CDP event name (e.g., 'Page.loadEventFired').
TYPE:
|
callback
|
Async function called when event occurs.
TYPE:
|
temporary
|
If True, callback removed after first trigger.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
Callback ID for later removal. |
Note
Corresponding CDP domain must be enabled before events fire.