📄 playwright-python/java/docs/api/class-websocket

File: class-websocket.md | Updated: 11/18/2025

Source: https://playwright.dev/java/docs/api/class-websocket

Skip to main content

On this page

The WebSocket class represents WebSocket connections within a page. It provides the ability to inspect and manipulate the data being transmitted and received.

If you want to intercept or modify WebSocket frames, consider using WebSocketRoute .


Methods


isClosed

Added before v1.9 webSocket.isClosed

Indicates that the web socket has been closed.

Usage

WebSocket.isClosed();

Returns


url

Added before v1.9 webSocket.url

Contains the URL of the WebSocket.

Usage

WebSocket.url();

Returns


waitForFrameReceived

Added in: v1.10 webSocket.waitForFrameReceived

Performs action and waits for a frame to be sent. If predicate is provided, it passes WebSocketFrame value into the predicate function and waits for predicate(webSocketFrame) to return a truthy value. Will throw an error if the WebSocket or Page is closed before the frame is received.

Usage

WebSocket.waitForFrameReceived(callback);WebSocket.waitForFrameReceived(callback, options);

Arguments

  • options WebSocket.WaitForFrameReceivedOptions (optional)

    • setPredicate Predicate <WebSocketFrame > (optional) Added in: v1.9#

      Receives the WebSocketFrame object and resolves to truthy value when the waiting should resolve.

    • setTimeout double (optional) Added in: v1.9#

      Maximum time to wait for in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout. The default value can be changed by using the BrowserContext.setDefaultTimeout() .

  • callback Runnable Added in: v1.9#

    Callback that performs the action triggering the event.

Returns


waitForFrameSent

Added in: v1.10 webSocket.waitForFrameSent

Performs action and waits for a frame to be sent. If predicate is provided, it passes WebSocketFrame value into the predicate function and waits for predicate(webSocketFrame) to return a truthy value. Will throw an error if the WebSocket or Page is closed before the frame is sent.

Usage

WebSocket.waitForFrameSent(callback);WebSocket.waitForFrameSent(callback, options);

Arguments

  • options WebSocket.WaitForFrameSentOptions (optional)

    • setPredicate Predicate <WebSocketFrame > (optional) Added in: v1.9#

      Receives the WebSocketFrame object and resolves to truthy value when the waiting should resolve.

    • setTimeout double (optional) Added in: v1.9#

      Maximum time to wait for in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout. The default value can be changed by using the BrowserContext.setDefaultTimeout() .

  • callback Runnable Added in: v1.9#

    Callback that performs the action triggering the event.

Returns


Events


onClose(handler)

Added before v1.9 webSocket.onClose(handler)

Fired when the websocket closes.

Usage

WebSocket.onClose(handler)

Event data


onFrameReceived(handler)

Added in: v1.9 webSocket.onFrameReceived(handler)

Fired when the websocket receives a frame.

Usage

WebSocket.onFrameReceived(handler)

Event data


onFrameSent(handler)

Added in: v1.9 webSocket.onFrameSent(handler)

Fired when the websocket sends a frame.

Usage

WebSocket.onFrameSent(handler)

Event data


onSocketError(handler)

Added in: v1.9 webSocket.onSocketError(handler)

Fired when the websocket has an error.

Usage

WebSocket.onSocketError(handler)

Event data