# Selenium（软件）

Selenium 是一个开源项目，旗下汇集了用于自动化网页浏览器的工具与库，最为人熟知的是其 WebDriver API——它像用户一样原生地驱动浏览器，既可在本地运行，也可在远程机器上运行。<sup>[1](https://www.selenium.dev/documentation/en/introduction/)</sup> 该项目包含若干独立工具：用于以编程方式控制浏览器的 WebDriver、用于录制和回放用户操作的 Selenium IDE，以及用于跨机器和平台并行运行测试的 Selenium Grid。<sup>[1](https://www.selenium.dev/documentation/en/introduction/)</sup> 其代码与文档以 Apache 2.0 许可证授权，版权归 [Software Freedom Conservancy](https://www.edgepedia.org/zh/software-freedom-conservancy)（一家 501(c)3 非营利组织）所有。<sup>[2](https://www.selenium.dev/documentation/about/copyright/)</sup><sup> • </sup><sup>[3](https://www.selenium.dev/project/governance/)</sup>

| 关键事实 | 详情 |
|---|---|
| 起源 | 2004 年由杰森·哈金斯（Jason Huggins）在芝加哥 ThoughtWorks 公司发起，最初名为“JavaScriptTestRunner”，用于测试一个内部的时间与费用报销应用<sup>[4](https://www.selenium.dev/history/)</sup> |
| 核心 API | WebDriver，自 2018 年 6 月 5 日起成为 W3C 推荐标准，定义了一套平台无关、语言无关的接口来控制网页浏览器<sup>[5](https://www.w3.org/TR/webdriver/all/)</sup> |
| 当前主要版本 | Selenium 4.0.0，由西蒙·斯图尔特（Simon Stewart）于 2021 年 10 月 13 日宣布发布，提供 Java、.NET、Python、Ruby 和 JavaScript 版本<sup>[6](https://www.selenium.dev/blog/2021/announcing-selenium-4/)</sup> |
| 驱动管理 | Selenium Manager，一个用 Rust 编写的命令行工具，自 4.6 版起随每个 Selenium 版本一同发布，可自动发现、下载并缓存浏览器驱动<sup>[7](https://www.selenium.dev/documentation/selenium_manager/)</sup> |
| Grid 架构 | Selenium Grid 4 是从零重写的版本，包含六个组件：Router、Distributor、Session Map、New Session Queue、Event Bus 和 Node<sup>[8](https://www.selenium.dev/documentation/grid/components/)</sup> |
| 双向协议 | WebDriver BiDi，即 W3C 标准的双向浏览器自动化协议，增加了 WebSocket 连接，使脚本能够流式接收并响应浏览器事件<sup>[9](https://www.selenium.dev/documentation/webdriver/)</sup> |
| 使用情况（2026 年调查） | 在 Stack Overflow 2026 年调查的受访者中，有 12.0% 使用 Selenium，落后于 Playwright 的 24.9%，领先于 Cypress 的 8.2%<sup>[10](https://survey.stackoverflow.co/2026/technology/data/code-test)</sup> |

## 历史

该项目始于 2004 年芝加哥的 ThoughtWorks 公司，杰森·哈金斯（Jason Huggins）在[保罗·格罗斯](https://www.edgepedia.org/zh/paul-gross)（Paul Gross）和王洁·蒂娜（Jie Tina Wang）的协助下构建了 Core 模式，名为“JavaScriptTestRunner”，用于测试一个用 Python 编写、运行在 Plone 上的内部时间与费用报销应用。<sup>[4](https://www.selenium.dev/history/)</sup> 同为 ThoughtWorks 员工的保罗·哈曼特（Paul Hammant） 很快开始讨论将该工具开源，并定义了一种“驱动”模式，即用户可以用自己选择的语言通过网络远程使用 Selenium，从而绕开浏览器的同源策略。<sup>[4](https://www.selenium.dev/history/)</sup>

**Selenium Remote Control。** 在 BEA 公司，丹·法布利奇（Dan Fabulich）和纳尔逊·斯普劳尔（Nelson Sproul）得出结论：驱动/服务器到浏览器的架构并非最灵活的方案，于是他们将驱动代码分叉为一个独立服务器，并捆绑了 MortBay 的 Jetty 作为 Web 代理。合并回主干后，这成为了 Selenium Remote Control（RC），旧的驱动代码线随之退役。<sup>[4](https://www.selenium.dev/history/)</sup> 由于各语言绑定通过 HTTP 远程控制浏览器，这种紧密模仿 Selenium Core 表格式语法的网络传输格式被称为“Selenese”。<sup>[11](https://aosabook.org/en/v1/selenium.html)</sup>

**Selenium IDE。** 日本的笠谷真也（Shinya Kasatani）意识到，他可以将核心代码封装为 Firefox 浏览器内的一个 IDE 模块，在同一插件中录制和回放测试；该工具不受同源策略的限制。<sup>[4](https://www.selenium.dev/history/)</sup> 如今 IDE 是一个独立的 Windows、macOS 和 Linux 应用程序，使用现有的 Selenium 命令录制用户在浏览器中的操作。<sup>[12](https://www.selenium.dev/documentation/ide/)</sup>

**WebDriver 的合并。** ThoughtWorks 的西蒙·斯图尔特（Simon Stewart）一直在开发 WebDriver，这是一个独立的工具，不依赖 [JavaScript](https://www.edgepedia.org/zh/javascript) 来完成繁重工作，而是为每个浏览器从头编写客户端，并提供比 Selenium RC 更高层的、面向对象的 API。<sup>[4](https://www.selenium.dev/history/)</sup><sup> • </sup><sup>[11](https://aosabook.org/en/v1/selenium.html)</sup> WebDriver 源于应用[适配器模式](https://www.edgepedia.org/zh/adapter-pattern)将端到端测试与底层测试工具隔离开来的工作，最初是 HtmlUnit 的封装。<sup>[11](https://aosabook.org/en/v1/selenium.html)</sup> 2009 年 8 月，两个项目宣布合并，Selenium WebDriver 由此诞生。<sup>[11](https://aosabook.org/en/v1/selenium.html)</sup> 杰森·哈金斯（Jason Huggins）于 2007 年离开 ThoughtWorks，加入了当时尚属秘密的谷歌（Google）内部 Selenium 支持团队，珍妮弗·贝文（Jennifer Bevan）和其他谷歌员工在那里为 Selenium RC 编写了自己的网格功能并在内部部署。<sup>[4](https://www.selenium.dev/history/)</sup>

## WebDriver

通过 Selenium 服务器，WebDriver 可以像人一样操作浏览器——无论是在本地还是远程机器上——该术语既指各语言绑定，也指针对每个浏览器的控制代码。<sup>[9](https://www.selenium.dev/documentation/webdriver/)</sup> W3C 规范将其定义为一个远程控制接口，能够对用户代理进行内省和控制，为进程外程序提供平台无关、语言无关的网络传输协议来指示网页浏览器。<sup>[13](https://www.w3.org/TR/webdriver2/)</sup> 该标准明确源自 Selenium WebDriver 框架，其设计目标包括兼容现有 Selenium 用户、对开发者和测试人员保持简单，以及为浏览器特定功能提供扩展机制。<sup>[13](https://www.w3.org/TR/webdriver2/)</sup>

**等待策略。** 浏览器自动化中的一个常见挑战是确保应用处于可执行命令的状态；浏览器与 Selenium 代码之间的竞态是测试不稳定的主要原因。<sup>[14](https://www.selenium.dev/documentation/webdriver/waits/)</sup> 除固定休眠外，Selenium 提供两种同步机制。隐式等待是一个全局设置，应用于会话中每次元素定位调用，默认值为 0；文档警告不要混用隐式等待和显式等待，因为这样做可能导致不可预测的等待时间。<sup>[14](https://www.selenium.dev/documentation/webdriver/waits/)</sup> 显式等待会在继续之前轮询应用是否满足特定条件，预期条件类描述了常见条件，如元素存在、可见，或标题包含某个值。<sup>[14](https://www.selenium.dev/documentation/webdriver/waits/)</sup><sup> • </sup><sup>[15](https://www.selenium.dev/documentation/webdriver/support_features/expected_conditions/)</sup> .NET 在 Selenium 4 中停止支持预期条件以减少维护负担，Ruby 则改用块、proc 和 lambda。<sup>[15](https://www.selenium.dev/documentation/webdriver/support_features/expected_conditions/)</sup>

**WebDriver BiDi。** WebDriver BiDi 由 Selenium 项目与浏览器厂商共同开发，是 W3C 标准化的双向浏览器自动化协议。它为 WebDriver 增加了 [WebSocket](https://www.edgepedia.org/zh/websocket) 连接，使脚本能够流式接收并响应浏览器事件，如网络请求、控制台消息和 JavaScript 错误，并作为 Chrome DevTools Protocol 的跨浏览器替代方案。<sup>[9](https://www.selenium.dev/documentation/webdriver/)</sup>

## Selenium Manager

Selenium 通过浏览器的原生支持来实现自动化，这需要在脚本和浏览器之间有一个驱动组件（chromedriver、geckodriver、msedgedriver 等）。多年来这是一个手动过程，Java 的 WebDriverManager 和 Python 的 webdriver-manager 等第三方驱动管理器填补了这一空白。<sup>[7](https://www.selenium.dev/documentation/selenium_manager/)</sup> Selenium Manager 是该项目的官方驱动管理器，用 Rust 实现并编译为 Windows、Linux 和 macOS 版本，自 4.6 版起随每个 Selenium 版本一同发布。<sup>[7](https://www.selenium.dev/documentation/selenium_manager/)</sup>

其自动驱动管理会在驱动不可用时于每个会话之前运行：它通过 shell 命令发现已安装的浏览器版本，从浏览器厂商的元数据端点解析出匹配的驱动版本，下载并解压该产物，然后存储到本地缓存 `~/.cache/selenium` 以供复用。<sup>[7](https://www.selenium.dev/documentation/selenium_manager/)</sup> 自 Selenium 4.11.0 起，它还执行自动浏览器管理，在本地未安装 Chrome、Firefox 和 Edge 时发现、下载并缓存这些浏览器的发行版。<sup>[7](https://www.selenium.dev/documentation/selenium_manager/)</sup> Selenium Manager 作为后备机制运行：用户仍可手动或使用第三方工具管理驱动。<sup>[7](https://www.selenium.dev/documentation/selenium_manager/)</sup>

## Selenium Grid

Selenium Grid 将来自客户端的命令转发到运行在远程机器上的浏览器实例，使 WebDriver 脚本能够在那些机器上执行；其目的是让跨多台机器并行运行测试变得简单，支持针对各种浏览器版本进行测试，并实现跨平台测试。<sup>[16](https://www.selenium.dev/documentation/grid/)</sup> Grid 4 是对先前版本从零重写的产物，专为容器化和云分布式扩展而设计。<sup>[8](https://www.selenium.dev/documentation/grid/components/)</sup>

该架构分为六个组件。<sup>[17](https://www.selenium.dev/documentation/grid/architecture/)</sup>

- **Router**，Grid 的前端，接收所有外部请求，并将新会话请求转发到 New Session Queue，将现有会话请求通过 Session Map 转发到正确的 Node。<sup>[8](https://www.selenium.dev/documentation/grid/components/)</sup>
- **New Session Queue** 保存尚未分配给 Node 的传入会话。<sup>[17](https://www.selenium.dev/documentation/grid/architecture/)</sup>
- **Distributor** 维护可用槽位的模型，并将新会话请求分配给与请求能力匹配的槽位。<sup>[17](https://www.selenium.dev/documentation/grid/architecture/)</sup>
- **Node** 运行 WebDriver 会话；每个会话占用一个槽位，每个 Node 有一个或多个槽位。<sup>[17](https://www.selenium.dev/documentation/grid/architecture/)</sup>
- **Session Map** 将会话 ID 映射到运行每个会话的 Node 地址。<sup>[17](https://www.selenium.dev/documentation/grid/architecture/)</sup>
- **Event Bus** 在其他组件之间传递异步消息。<sup>[17](https://www.selenium.dev/documentation/grid/architecture/)</sup>

槽位带有一个“刻板印象”，即新会话请求在被 [Distributor](https://www.edgepedia.org/zh/distributor) 路由到所属 Node 之前必须匹配的最小能力集；一个 Node 可以公布的槽位数多于其并发运行能力，例如最大会话数为 10，同时为 Chrome、Edge 和 Firefox 各设 10 个槽位。<sup>[17](https://www.selenium.dev/documentation/grid/architecture/)</sup> 组件之间通过同步的 JSON-over-HTTP 请求通信，用于需要响应的场景（如大多数 WebDriver 调用），以及通过 Event Bus 上的异步事件进行广播。<sup>[17](https://www.selenium.dev/documentation/grid/architecture/)</sup> Node 通过发出心跳事件来注册；Distributor 通过查询 Node 的 `/status` 端点来响应，并继续定期轮询。<sup>[17](https://www.selenium.dev/documentation/grid/architecture/)</sup> Grid 可以部署为单一独立服务器、中心-节点模式，或完全分布式模式，每个组件单独启动，最好部署在不同机器上。<sup>[18](https://www.selenium.dev/documentation/grid/getting_started/)</sup>

## 生态系统与采用情况

Selenium 项目是 Software Freedom Conservancy 的成员，该组织与其他项目（如 [Inkscape](https://www.edgepedia.org/zh/inkscape)、Samba 和 Wine）共享组织资源。<sup>[3](https://www.selenium.dev/project/governance/)</sup> 围绕它成长起一个庞大的开源项目生态系统，包括第三方驱动、绑定、插件和框架，它们都将 Selenium WebDriver 作为其工作的核心部分。<sup>[19](https://www.selenium.dev/about/)</sup> 学术研究直接考察了其性能：2016 年一项在四种浏览器和多种语言绑定上对比 Selenium 与 Watir 的基准测试发现，综合考虑执行时间、内存占用、脚本长度和稳定性，最佳配置是 Selenium 配合 Python 绑定在 Chrome 上运行。<sup>[20](https://arxiv.org/abs/1611.00578)</sup>

在 Stack Overflow 2026 年开发者调查中，12.0% 的受访者报告使用 Selenium，相比之下 [Playwright](https://www.edgepedia.org/zh/playwright) 为 24.9%，Cypress 为 8.2%。<sup>[10](https://survey.stackoverflow.co/2026/technology/data/code-test)</sup> 比较性文章将 Selenium 的架构描述为基于 W3C WebDriver 或 BiDi 协议的客户端-服务器模式，适合企业级、跨语言、遗留系统或移动端测试，基于网络的控制相对于进程内工具会引入中等程度的延迟。<sup>[21](https://stackoverflow.blog/2026/06/15/selenium-vs-cypress-vs-playwright-choosing-your-test-automation-framework/)</sup>

## References

1. [Selenium Overview, Selenium documentation](https://www.selenium.dev/documentation/en/introduction/)
2. [Copyright and attributions, Selenium documentation](https://www.selenium.dev/documentation/about/copyright/)
3. [Selenium Project Governance](https://www.selenium.dev/project/governance/)
4. [Selenium History](https://www.selenium.dev/history/)
5. [WebDriver, W3C Recommendation, 5 June 2018](https://www.w3.org/TR/webdriver/all/)
6. [Announcing Selenium 4, Simon Stewart, 13 October 2021](https://www.selenium.dev/blog/2021/announcing-selenium-4/)
7. [Selenium Manager, Selenium documentation](https://www.selenium.dev/documentation/selenium_manager/)
8. [Selenium Grid Components, Selenium documentation](https://www.selenium.dev/documentation/grid/components/)
9. [WebDriver, Selenium documentation](https://www.selenium.dev/documentation/webdriver/)
10. [Code Testing tools, Stack Overflow Developer Survey 2026](https://survey.stackoverflow.co/2026/technology/data/code-test)
11. [Selenium WebDriver, The Architecture of Open Source Applications, Volume 1](https://aosabook.org/en/v1/selenium.html)
12. [Selenium IDE, Selenium documentation](https://www.selenium.dev/documentation/ide/)
13. [WebDriver, W3C Working Draft (webdriver2)](https://www.w3.org/TR/webdriver2/)
14. [Waiting Strategies, Selenium documentation](https://www.selenium.dev/documentation/webdriver/waits/)
15. [Waiting with Expected Conditions, Selenium documentation](https://www.selenium.dev/documentation/webdriver/support_features/expected_conditions/)
16. [Grid, Selenium documentation](https://www.selenium.dev/documentation/grid/)
17. [Grid architecture, Selenium documentation](https://www.selenium.dev/documentation/grid/architecture/)
18. [Getting started with Selenium Grid, Selenium documentation](https://www.selenium.dev/documentation/grid/getting_started/)
19. [About Selenium](https://www.selenium.dev/about/)
20. [Benchmarking Web-testing: Selenium versus Watir and the Choice of Programming Language and Browser, arXiv:1611.00578](https://arxiv.org/abs/1611.00578)
21. [Selenium vs. Cypress vs Playwright: Choosing your test automation framework, Stack Overflow Blog](https://stackoverflow.blog/2026/06/15/selenium-vs-cypress-vs-playwright-choosing-your-test-automation-framework/)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Build, deployment, and development tooling

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/selenium-software

English original: https://www.edgepedia.org/selenium-software
