大网数据广告活动
Web 标准与前端开发

Create a WebDriver BiDi connection

· 52CE 我爱测

The client and the browser communicate using the WebDriver BiDi protocol over a WebSocket connection. There are two ways a client can establish this connection.

The client and the browser communicate using the WebDriver BiDi protocol over a WebSocket connection. There are two ways a client can establish this connection.

In one method, when creating a classic WebDriver session, the WebDriver client sets the webSocketUrl capability to true to request BiDi to be enabled; the client then starts the browser with the WebSocket port open.

In the other method, the WebDriver client starts the browser from the command line by passing the required flag and the desired port. This method works with Firefox directly; however, Chromium-based browsers require the additional Chromium BiDi wrapper package. The sections in this article walk you through this method using Firefox.

Launching the browser

To use WebDriver BiDi, you need to enable it in the browser by launching it with the --remote-debugging-port flag. The browser then listens for incoming WebSocket connections on the specified port. Port 9222 is a conventional default for browser debugging, but you can use any available port, or specify 0 to let the system assign a free port automatically.

firefox --remote-debugging-port 9222

On macOS, firefox may not be in your PATH. In that case, use the full path instead:

/Applications/Firefox.app/Contents/MacOS/firefox --remote-debugging-port 9222

Getting the WebSocket URL

Firefox exposes the BiDi WebSocket directly at:

ws://127.0.0.1:PORT/session

For example, if you launched Firefox with --remote-debugging-port 9222, the URL is ws://127.0.0.1:9222/session. If you specified port 0, check the stderr output for a message like WebDriver BiDi listening on ws://127.0.0.1:46249 to find the assigned port.

[!NOTE] Firefox uses 127.0.0.1 rather than localhost for the BiDi WebSocket endpoint.

Connecting to the WebSocket endpoint

With the WebSocket URL, use any WebSocket client to open a connection. Common options include the ws package for Node.js and the websockets package for Python.

Once connected, you can send WebDriver BiDi commands as JSON messages and receive responses and events from the browser. See session.new to create a BiDi session after connecting.

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。