WebSocket
WebSocket 类表示页面内的 WebSocket 连接。它提供了检查和操作正在发送和接收的数据的能力。
🌐 The WebSocket class represents WebSocket connections within a page. It provides the ability to inspect and manipulate the data being transmitted and received.
如果你想拦截或修改 WebSocket 帧,可以考虑使用 WebSocketRoute。
🌐 If you want to intercept or modify WebSocket frames, consider using WebSocketRoute.
方法
🌐 Methods
expect_event
Added before v1.9等待事件触发并将其值传递给谓词函数。当谓词返回真值时返回。如果在事件触发前 WebSocket 被关闭,将抛出错误。返回事件数据值。
🌐 Waits for event to fire and passes its value into the predicate function. Returns when the predicate returns truthy value. Will throw an error if the webSocket is closed before the event is fired. Returns the event data value.
用法
web_socket.expect_event(event)
web_socket.expect_event(event, **kwargs)
参数
-
事件名称,相同的会传入
webSocket.on(event)。 -
predicateCallable (optional)#接收事件数据并在等待解决时解析为真值。
-
等待的最长时间(以毫秒为单位)。默认为
30000(30 秒)。传入0可禁用超时。可以使用 browser_context.set_default_timeout() 来更改默认值。
返回
wait_for_event
Added before v1.9在大多数情况下,你应该使用 web_socket.expect_event()。
🌐 In most cases, you should use web_socket.expect_event().
等待给定的 event 触发。如果提供了谓词,它会将事件的值传入 predicate 函数,并等待 predicate(event) 返回一个真值。如果在 event 触发之前套接字已关闭,将抛出错误。
🌐 Waits for given event to fire. If predicate is provided, it passes event's value into the predicate function and waits for predicate(event) to return a truthy value. Will throw an error if the socket is closed before the event is fired.
用法
web_socket.wait_for_event(event)
web_socket.wait_for_event(event, **kwargs)
参数
-
事件名称,通常传递到
*.on(event)的那个相同名称。 -
predicateCallable (optional)#接收事件数据并在等待解决时解析为真值。
-
等待的最长时间(以毫秒为单位)。默认为
30000(30 秒)。传入0可禁用超时。可以使用 browser_context.set_default_timeout() 来更改默认值。
返回
属性
🌐 Properties
is_closed
Added before v1.9表示 Web 套接字已关闭。
🌐 Indicates that the web socket has been closed.
用法
web_socket.is_closed()
返回
url
Added before v1.9包含 WebSocket 的 URL。
🌐 Contains the URL of the WebSocket.
用法
web_socket.url
返回
事件
🌐 Events
on("close")
Added before v1.9当 websocket 关闭时触发。
🌐 Fired when the websocket closes.
用法
web_socket.on("close", handler)
事件数据
on("framereceived")
Added in: v1.9当 websocket 收到帧时触发。
🌐 Fired when the websocket receives a frame.
用法
web_socket.on("framereceived", handler)
事件数据
- [字符串] | [字节]
on("framesent")
Added in: v1.9当 websocket 发送帧时触发。
🌐 Fired when the websocket sends a frame.
用法
web_socket.on("framesent", handler)
事件数据
- [字符串] | [字节]
on("socketerror")
Added in: v1.9当 websocket 出现错误时触发。
🌐 Fired when the websocket has an error.
用法
web_socket.on("socketerror", handler)
事件数据