RobotFramework Selenium与Browser常用关键字对比

一、核心区别概述

Browser 库和 SeleniumLibrary 在 Robot Framework 中用法非常相似,关键区别在于底层驱动机制:SeleniumLibrary 基于 Selenium WebDriver 驱动浏览器,而 Browser 库基于 Playwright 驱动浏览器。这一技术栈差异带来了以下几个方面的影响:

| 对比维度 | SeleniumLibrary | Browser 库 |

| :------------------ | :-------------------------------------------------- | :------------------------------------------ |

| **底层技术** | Selenium WebDriver | Playwright (Node.js) |

| **浏览器驱动** | 需要独立的 WebDriver | 内置浏览器二进制文件,无需额外驱动 |

| **执行速度** | 基准速度 | 测试执行速度提升约 50% |

| **浏览器支持** | Chrome, Firefox, Edge, Safari | Chromium, Firefox, WebKit, Edge(原生支持) |

| **自动等待机制** | 需要显式或隐式等待(约 40% 测试失败由时序问题导致) | 内置智能自动等待 |

| **Shadow DOM 支持** | 不支持,需要复杂变通方案 | 原生支持穿透 Shadow DOM |

| **上下文隔离** | 每个浏览器需独立进程 | 支持多上下文,比 Selenium 快约 10 倍 |

| **关键字总数** | 约 180+ 个 | 约 118+ 个 |

| **生态系统** | 成熟,资源丰富 | 较新,资源相对少 |

> **注意**:两个库不能混用------不能用 SeleniumLibrary 打开浏览器后用 Browser 库的关键字操作,必须完全使用其中一种。

二、浏览器与窗口管理

2.1 打开浏览器

| 功能 | SeleniumLibrary | Browser 库 |

| :----------------- | :----------------------------- | :----------------------------------------------------------- |

| 打开浏览器 | `Open Browser` `url` `browser` | `New Browser` `browser` `headless=False` + `New Context` + `New Page` `url` |

| 快速打开(一次性) | --- | `Open Browser` `url` `browser`(相当于三合一便捷方法) |

| 创建无头浏览器 | 添加参数 `headlesschrome` 等 | `New Browser` 默认 headless=True |

| 创建持久化上下文 | --- | `New Persistent Context` `url` `browser` |

SeleniumLibrary 中 `Open Browser` 一次性打开浏览器并导航到指定 URL。Browser 库采用 Browser → Context → Page 三层架构设计,更灵活但也更复杂:

robotframework

```

SeleniumLibrary 写法

*** Settings ***

Library SeleniumLibrary

*** Test Cases ***

Open Google

Open Browser https://www.google.com chrome

Browser 库写法

*** Settings ***

Library Browser

*** Test Cases ***

Open Google

New Browser chromium headless=false

New Context viewport={'width': 1920, 'height': 1080}

New Page https://www.google.com

```

Browser 库也提供了 `Open Browser` 关键字作为便捷方法,等价于一次性执行 `New Browser` + `New Context` + `New Page`,适用于快速实验或调试场景。

2.2 关闭浏览器/窗口

| 功能 | SeleniumLibrary | Browser 库 |

| :------------- | :------------------- | :-------------------- |

| 关闭当前浏览器 | `Close Browser` | `Close Browser` |

| 关闭所有浏览器 | `Close All Browsers` | `Close Browser` `ALL` |

| 关闭当前页面 | `Close Window` | `Close Page` |

| 关闭当前上下文 | --- | `Close Context` |

2.3 窗口/标签页切换

| 功能 | SeleniumLibrary | Browser 库 |

| :--------------- | :---------------------------- | :------------------------------------- |

| 切换窗口 | `Switch Window` `locator=NEW` | `Switch Page` `NEW` |

| 通过标题切换 | `Switch Window` `title=xxx` | `Switch Page` `url=xxx` 或 `title=xxx` |

| 获取窗口句柄列表 | `Get Window Handles` | `Get Page Ids` |

| 获取当前窗口信息 | `Get Window Titles` | `Get Title` |

2.4 页面导航

| 功能 | SeleniumLibrary | Browser 库 |

| :----------- | :------------------------- | :------------------- |

| 跳转到 URL | `Go To` `url` | `Go To` `url` |

| 刷新页面 | `Reload Page` | `Reload` |

| 后退 | `Go Back` | `Go Back` |

| 前进 | --- | `Go Forward` |

| 获取当前 URL | `Get Location` | `Get Url` |

| 验证 URL | `Location Should Be` `url` | `Get Url` `==` `url` |

三、元素交互

3.1 点击操作

| 功能 | SeleniumLibrary | Browser 库 |

| :--------------- | :------------------------------- | :-------------------------------- |

| 点击元素(通用) | `Click Element` `locator` | `Click` `selector` |

| 点击按钮 | `Click Button` `locator` | `Click` `selector`(通用) |

| 点击链接 | `Click Link` `locator` | `Click` `selector`(通用) |

| 双击元素 | `Double Click Element` `locator` | `Double Click` `selector` |

| 右键点击 | `Open Context Menu` `locator` | `Click` `selector` `button=right` |

| 点击坐标 | `Click Element At Coordinates` | `Click` `x=...` `y=...` |

| 点击图片 | `Click Image` `locator` | 使用 `Click` 配合图片选择器 |

| 选中复选框 | `Select Checkbox` `locator` | `Check Checkbox` `selector` |

| 取消复选框 | `Unselect Checkbox` `locator` | `Uncheck Checkbox` `selector` |

| 选择单选按钮 | `Select Radio Button` | `Click` `selector` |

Browser 库的 `Click` 关键字是统一的点击入口,不再区分 Button、Link、Element 等类型,这简化了 API 但丢失了一些语义信息。

3.2 文本输入

| 功能 | SeleniumLibrary | Browser 库 |

| :------------------- | :------------------------------------ | :----------------------------------------------------------- |

| 输入文本 | `Input Text` `locator` `text` | `Fill Text` `selector` `text` 或 `Type Text` `selector` `text` |

| 输入密码 | `Input Password` `locator` `password` | `Fill Secret` `selector` `password` |

| 清空输入框 | `Clear Element Text` `locator` | `Clear Text` `selector` |

| 模拟键盘按键 | `Press Keys` `locator` `keys` | `Keyboard Key` `press` `key` |

| 逐字输入(模拟打字) | --- | `Type Text` `selector` `text` `delay=100ms` |

`Fill Text` 会先清空输入框再填入内容;`Type Text` 则逐个字符输入,可设置延迟来模拟真实用户打字行为。

3.3 下拉列表选择

| 功能 | SeleniumLibrary | Browser 库 |

| :------------- | :-------------------------- | :----------------------------------------------- |

| 按值选择 | `Select From List By Value` | `Select Options By` `selector` `value` |

| 按标签选择 | `Select From List By Label` | `Select Options By` `selector` `label` |

| 按索引选择 | `Select From List By Index` | `Select Options By` `selector` `index` |

| 获取选中项标签 | `Get Selected List Label` | `Get Selected Options` `selector` `then` `label` |

| 获取选中项值 | `Get Selected List Value` | `Get Selected Options` `selector` `then` `value` |

| 取消选择 | `Unselect From List By ...` | `Deselect Options` `selector` |

3.4 文件上传

| 功能 | SeleniumLibrary | Browser 库 |

| :----------- | :---------------------------------- | :----------------------------------- |

| 选择文件上传 | `Choose File` `locator` `file_path` | `Upload File` `selector` `file_path` |

3.5 鼠标操作

| 功能 | SeleniumLibrary | Browser 库 |

| :----------- | :-------------------------- | :---------------------------------------------- |

| 鼠标悬停 | `Mouse Over` `locator` | `Hover` `selector` |

| 拖拽 | `Drag And Drop` `from` `to` | `Drag And Drop` `from` `to` |

| 拖拽偏移 | `Drag And Drop By Offset` | `Drag And Drop By Offset` |

| 鼠标按下 | `Mouse Down` `locator` | `Mouse Button` `down` |

| 鼠标松开 | `Mouse Up` `locator` | `Mouse Button` `up` |

| 鼠标移动 | --- | `Mouse Move` `x` `y` |

| 虚拟鼠标控制 | --- | `Mouse Move` 和 `Mouse Button` 配合实现虚拟鼠标 |

四、元素查找与获取信息

| 功能 | SeleniumLibrary | Browser 库 |

| :----------- | :--------------------------------------- | :-------------------------------- |

| 获取文本 | `Get Text` `locator` | `Get Text` `selector` |

| 获取属性 | `Get Element Attribute` `locator` `attr` | `Get Attribute` `selector` `attr` |

| 获取元素数量 | `Get Element Count` `locator` | `Get Element Count` `selector` |

| 获取元素 | `Get WebElement` `locator` | `Get Element` `selector` |

| 获取元素列表 | `Get WebElements` `locator` | `Get Elements` `selector` |

| 获取页面标题 | `Get Title` | `Get Title` |

| 获取页面源码 | `Get Source` | `Get Page Source` |

选择器(定位符)策略对比

两个库在选择器语法上略有差异:

| 选择器类型 | SeleniumLibrary | Browser 库 |

| :---------- | :----------------------------- | :------------------------------------- |

| CSS(默认) | `css=.class` 或直接写 `.class` | 直接写 `.class`(默认 CSS) |

| XPath | `xpath=//div` 或以 `//` 开头 | 以 `//` 开头自动识别为 XPath |

| ID | `id=foo` | `#foo`(CSS)或 `id=foo` |

| Name | `name=foo` | `[name="foo"]`(CSS) |

| 文本匹配 | --- | `text="Login"` 或 `"Login"` |

| 链式选择器 | 不支持原生 | 支持 `>>` 链式(如 `#iframe >> #btn`) |

Browser 库的选择器功能更强大,支持文本引擎 `text="Login"` 直接按文本定位,以及 `>>` 链式选择器来穿透 Shadow DOM 和 iframe。

五、等待机制

| 功能 | SeleniumLibrary | Browser 库 |

| :--------------- | :---------------------------------- | :----------------------------------------------- |

| 设置隐式等待 | `Set Selenium Implicit Wait` | `Set Browser Timeout` |

| 等待元素可见 | `Wait Until Element Is Visible` | `Wait For Elements State` `selector` `visible` |

| 等待元素不可见 | `Wait Until Element Is Not Visible` | `Wait For Elements State` `selector` `hidden` |

| 等待元素启用 | `Wait Until Element Is Enabled` | `Wait For Elements State` `selector` `enabled` |

| 等待包含元素 | `Wait Until Page Contains Element` | `Wait For Elements State` `selector` `attached` |

| 等待页面包含文本 | `Wait Until Page Contains` | `Wait For Elements State` `text="xxx"` `visible` |

| 等待条件为真 | --- | `Wait For Condition` `condition` |

| 等待网络空闲 | --- | `Wait For Load State` `networkidle` |

| 固定等待 | `Sleep` `seconds` | `Sleep` `seconds` |

Browser 库的最大优势之一是其内置的自动等待机制:交互前会自动等待元素可操作,因此通常不需要编写大量显式等待语句,大大减少了测试的不稳定性。

robotframework

```

SeleniumLibrary 典型写法

Click Element id=submit-btn

Wait Until Element Is Visible css=.success-message

Element Text Should Be css=.success-message Operation successful

Browser 库写法(自动等待)

Click id=submit-btn

Get Text css=.success-message == Operation successful

```

六、断言与验证

| 功能 | SeleniumLibrary | Browser 库 |

| :--------------- | :---------------------------------- | :--------------------------------------------- |

| 页面应包含文本 | `Page Should Contain` `text` | `Get Text` `body` `contains` `text` |

| 页面不应包含文本 | `Page Should Not Contain` `text` | `Get Text` `body` `not contains` `text` |

| 元素应可见 | `Element Should Be Visible` | `Get Element State` `selector` `==` `visible` |

| 元素应不可见 | `Element Should Not Be Visible` | `Get Element State` `selector` `==` `hidden` |

| 元素应启用 | `Element Should Be Enabled` | `Get Element State` `selector` `==` `enabled` |

| 元素应禁用 | `Element Should Be Disabled` | `Get Element State` `selector` `==` `disabled` |

| 元素文本应为 | `Element Text Should Be` `text` | `Get Text` `selector` `==` `text` |

| 元素应包含 | `Element Should Contain` `text` | `Get Text` `selector` `contains` `text` |

| 复选框应选中 | `Checkbox Should Be Selected` | `Get Checkbox State` `selector` `==` `checked` |

| 属性值应为 | `Element Attribute Value Should Be` | `Get Attribute` `selector` `attr` `==` `value` |

Browser 库采用了一种更统一的断言模式:关键字返回状态值,然后使用 `==`、`contains`、`should be` 等运算符进行断言,支持 `validate` 和 `then` 闭包进行更复杂的条件判断。

robotframework

```

Browser 库的断言模式示例

Get Text css=h1 == Welcome

Get Title == Home Page

Get Url contains /dashboard

Get Element State #submit-btn == enabled

```

七、弹窗与对话框

| 功能 | SeleniumLibrary | Browser 库 |

| :----------------- | :------------------------------- | :------------------------------------------ |

| 处理 Alert | `Handle Alert` `action=ACCEPT` | `Handle Future Dialogs` `action=accept` |

| Alert 文本应为 | `Alert Should Be Present` `text` | 结合 `Handle Future Dialogs` 和 `promise` |

| 获取 Alert 文本 | `Get Alert Message` | 使用 Promise 方式 |

| 输入 Alert 文本 | `Input Text Into Alert` | `Handle Future Dialogs` `prompt_input=text` |

| 确认 Alert 不存在 | `Alert Should Not Be Present` | --- |

| 处理文件选择对话框 | --- | `Handle Future Dialogs` `action=accept` |

Browser 库对弹窗的处理采用"预注册"模式------必须在触发弹窗的操作之前调用 `Handle Future Dialogs`,这与 SeleniumLibrary 的事后处理模式不同:

robotframework

```

Browser 库弹窗处理模式

Handle Future Dialogs action=accept

Click #trigger-alert-btn

```

八、框架与多文档

| 功能 | SeleniumLibrary | Browser 库 |

| :---------------- | :------------------------------------ | :--------------------------------------------------- |

| 选择 Frame | `Select Frame` `locator` | 链式选择器 `#iframe >> #element` 或 `Switch Context` |

| 取消选择 Frame | `Unselect Frame` | 链式选择器默认只对单次操作有效 |

| 当前 Frame 应包含 | `Current Frame Should Contain` `text` | `Get Text` `body` `contains` `text` |

| 穿透 Shadow DOM | 不支持,需要复杂脚本 | 原生支持,CSS 选择器自动穿透 |

Browser 库处理 iframe 和 Shadow DOM 的最大优势是无需显式切换上下文。通过 `>>` 链式选择器可以直接定位到嵌套元素:

robotframework

```

Browser 库穿透 iframe 和 Shadow DOM 的方式

Click #iframe-id >> #inner-button

Click #shadow-host >> #shadow-element

```

九、Cookie 管理

| 功能 | SeleniumLibrary | Browser 库 |

| :------------------ | :-------------------------- | :------------------------ |

| 获取 Cookies | `Get Cookies` | `Get Cookies` |

| 获取单个 Cookie | `Get Cookie` `name` | `Get Cookie` `name` |

| 添加 Cookie | `Add Cookie` `name` `value` | `Add Cookie` `name=value` |

| 删除 Cookie | `Delete Cookie` `name` | `Delete Cookie` `name` |

| 删除所有 Cookies | `Delete All Cookies` | `Delete All Cookies` |

| 保存 Cookies 到文件 | --- | `Save Storage State` |

| 加载 Cookies 从文件 | --- | `Load Storage State` |

Browser 库新增的 `Save Storage State` / `Load Storage State` 可以保存和恢复完整的会话状态(包括 cookies 和 localStorage),便于实现登录态复用。

十、截图与调试

| 功能 | SeleniumLibrary | Browser 库 |

| :----------- | :------------------------------------- | :-------------------------------------------- |

| 截取页面截图 | `Capture Page Screenshot` `filename` | `Take Screenshot` `filename` |

| 截取元素截图 | `Capture Element Screenshot` `locator` | `Take Screenshot` `selector=xxx` |

| 测试录像 | 不支持 | `New Context` `recordVideo={'dir': 'videos'}` |

| 追踪调试 | 不支持 | `New Context` 启用 tracing 功能 |

| 获取页面 PDF | --- | `Get Pdf` |

Browser 库在调试和测试可观测性方面更强大,支持测试录像和 Playwright Trace Viewer,便于排查问题。

十一、JavaScript 执行

| 功能 | SeleniumLibrary | Browser 库 |

| :--------------------- | :-------------------------------- | :----------------------------------------- |

| 执行 JavaScript | `Execute Javascript` `code` | `Execute JavaScript` `code` |

| 执行异步 JavaScript | `Execute Async Javascript` `code` | `Execute JavaScript`(支持 Promise) |

| 获取 JavaScript 返回值 | 直接返回 | `Execute JavaScript` `code` `then` `value` |

Browser 库的 `Execute JavaScript` 函数参数可以是匿名函数或函数名,返回 Promise 时自动等待。

十二、网络拦截与 Mock

| 功能 | SeleniumLibrary | Browser 库 |

| :------------ | :--------------------- | :--------------------------------------- |

| 拦截请求 | 不支持 | `Route` `url` `handler` |

| Mock 响应 | 不支持 | `Route` `url` `fulfill` |

| 继续请求 | 不支持 | `Route` `url` `continue` |

| 终止请求 | 不支持 | `Route` `url` `abort` |

| 等待请求/响应 | 不支持 | `Wait For Request` / `Wait For Response` |

| 获取网络日志 | 部分支持(需额外配置) | `Promise To` `Wait For Request` |

这是 Browser 库的独特功能,内置网络拦截和请求 Mock 能力,可以模拟 API 响应、测试网络错误场景,而 SeleniumLibrary 需要借助外部代理工具才能实现。

十三、总结与选择建议

选择 SeleniumLibrary 的场景:

  • 项目已有大量 SeleniumLibrary 测试用例,且运行稳定

  • 需要与 Selenium Grid 集成实现分布式测试

  • 团队对 Selenium 生态非常熟悉

  • 测试老旧浏览器版本(Playwright 仅支持较新版本)

  • 需要更丰富的第三方集成和社区资源

选择 Browser 库的场景:

  • 新项目,没有历史包袱

  • 测试 React、Vue 等现代框架(需处理 Shadow DOM)

  • 追求更快的执行速度和更高的稳定性

  • 需要录像、trace 等调试功能

  • 需要网络拦截/Mock 能力

  • 希望减少 flaky tests 和等待代码

迁移建议

如需从 SeleniumLibrary 迁移到 Browser 库,可以使用 `robotframework-browser-migration` 工具包:它包含一个分析脚本(统计项目中使用的 SeleniumLibrary 关键字)和一个迁移层库 `SeleniumLibraryToBrowser`,该库使用 Browser 库实现了约 80% 的 SeleniumLibrary 关键字,可以大幅降低迁移工作量。

相关推荐
测试19981 天前
软件测试之持续集成
自动化测试·软件测试·python·功能测试·测试工具·测试用例·持续集成
qq_452396231 天前
第二篇:《主流UI自动化工具横向对比:Selenium、Cypress、Playwright、Puppeteer》
selenium·ui·自动化
七夜zippoe1 天前
OpenClaw 浏览器自动化实战
运维·chrome·自动化·浏览器·playwright·openclaw
Cd ...1 天前
RobotFramework Browser库找不到元素(本地测试和服务器不一致)
自动化测试·测试工具·playwright
测试19982 天前
2026最新软件测试面试八股文【附文档】
自动化测试·软件测试·python·测试工具·面试·职场和发展·测试用例
迷藏4942 天前
# 发散创新:基于Selenium的自动化测试框架重构与实战优化在当今快速迭代的软件开
java·python·selenium·测试工具·重构
willhuo2 天前
# 自动化数据采集技术研究与实现:基于Playwright的抖音网页自动化方案
运维·selenium·c#·自动化·chrome devtools·webview
阿尔泰科技官方2 天前
精准捕捉・高速传输 —— 24位采集+千兆以太网,全场景动态信号采集优选方案!
自动化测试·科技·自动化·信号处理·数据采集卡