
- Selenium 教程
- Selenium - 首页
- Selenium - 概述
- Selenium - 组件
- Selenium - 自动化测试
- Selenium - 环境设置
- Selenium - 远程控制
- Selenium IDE 教程
- Selenium - IDE 简介
- Selenium - 特性
- Selenium - 限制
- Selenium - 安装
- Selenium - 创建测试
- Selenium - 创建脚本
- Selenium - 控制流
- Selenium - 存储变量
- Selenium - 警报和弹出窗口
- Selenium - Selenese 命令
- Selenium - Actions 命令
- Selenium - Accessors 命令
- Selenium - Assertions 命令
- Selenium - Assert/Verify 方法
- Selenium - 定位策略
- Selenium - 脚本调试
- Selenium - 验证点
- Selenium - 模式匹配
- Selenium - JSON 数据文件
- Selenium - 浏览器执行
- Selenium - 用户扩展
- Selenium - 代码导出
- Selenium - 代码输出
- Selenium - JavaScript 函数
- Selenium - 插件
- Selenium WebDriver 教程
- Selenium - 简介
- Selenium WebDriver vs RC
- Selenium - 安装
- Selenium - 第一个测试脚本
- Selenium - 驱动程序会话
- Selenium - 浏览器选项
- Selenium - Chrome 选项
- Selenium - Edge 选项
- Selenium - Firefox 选项
- Selenium - Safari 选项
- Selenium - 双击
- Selenium - 右键单击
- Python 中的 HTML 报告
- 处理编辑框
- Selenium - 单个元素
- Selenium - 多个元素
- Selenium Web 元素
- Selenium - 文件上传
- Selenium - 定位器策略
- Selenium - 相对定位器
- Selenium - 查找器
- Selenium - 查找所有链接
- Selenium - 用户交互
- Selenium - WebElement 命令
- Selenium - 浏览器交互
- Selenium - 浏览器命令
- Selenium - 浏览器导航
- Selenium - 警报和弹出窗口
- Selenium - 处理表单
- Selenium - 窗口和选项卡
- Selenium - 处理链接
- Selenium - 输入框
- Selenium - 单选按钮
- Selenium - 复选框
- Selenium - 下拉框
- Selenium - 处理 IFrame
- Selenium - 处理 Cookie
- Selenium - 日期时间选择器
- Selenium - 动态 Web 表格
- Selenium - Actions 类
- Selenium - Action 类
- Selenium - 键盘事件
- Selenium - 键向上/向下
- Selenium - 复制和粘贴
- Selenium - 处理特殊键
- Selenium - 鼠标事件
- Selenium - 拖放
- Selenium - 笔事件
- Selenium - 滚动操作
- Selenium - 等待策略
- Selenium - 显式/隐式等待
- Selenium - 支持功能
- Selenium - 多选
- Selenium - 等待支持
- Selenium - 选择支持
- Selenium - 颜色支持
- Selenium - ThreadGuard
- Selenium - 错误和日志记录
- Selenium - 异常处理
- Selenium - 杂项
- Selenium - 处理 Ajax 调用
- Selenium - JSON 数据文件
- Selenium - CSV 数据文件
- Selenium - Excel 数据文件
- Selenium - 跨浏览器测试
- Selenium - 多浏览器测试
- Selenium - 多窗口测试
- Selenium - JavaScript 执行器
- Selenium - 无头执行
- Selenium - 捕获屏幕截图
- Selenium - 捕获视频
- Selenium - 页面对象模型
- Selenium - 页面工厂
- Selenium - 记录和回放
- Selenium - 框架
- Selenium - 浏览上下文
- Selenium - DevTools
- Selenium Grid 教程
- Selenium - 概述
- Selenium - 架构
- Selenium - 组件
- Selenium - 配置
- Selenium - 创建测试脚本
- Selenium - 测试执行
- Selenium - 端点
- Selenium - 自定义节点
- Selenium 报告工具
- Selenium - 报告工具
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium & 其他技术
- Selenium - Java 教程
- Selenium - Python 教程
- Selenium - C# 教程
- Selenium - Javascript 教程
- Selenium - Kotlin 教程
- Selenium - Ruby 教程
- Selenium - Maven 和 Jenkins
- Selenium - 数据库测试
- Selenium - LogExpert 日志记录
- Selenium - Log4j 日志记录
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash 测试
- Selenium - Apache Ant
- Selenium - Github 教程
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
Selenium IDE - 定位策略
Selenium IDE 拥有多种定位策略,有助于自动化测试用例。但是,Selenium IDE 默认情况下无法识别网页上的元素。为了定位元素,我们需要借助定位器,例如 id、name、css 和 xpath。
除了上述定位器之外,旧版本的 Selenium IDE 还支持其他定位器,例如 DOM 和 identifier,但是它们在最新版本中已被弃用。
示例
让我们以以下页面为例,在 First Name: 标签旁边输入文本 Selenium。这可以通过首先使用各种定位器定位元素来实现。

识别 Web 元素
右键单击网页,然后在 Chrome 浏览器中单击“检查”按钮,执行此操作后,网页的 HTML 代码将可访问。要检查页面上的元素,请单击下面突出显示的向左向上箭头。

一旦我们单击并将箭头指向输入框(在下面的图像中突出显示),它的 HTML 代码就会出现。

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
Selenium IDE 中定位元素的功能
在 Selenium IDE 中录制后创建测试后,我们就可以通过单击它来获取该测试中任何特定步骤的详细信息。单击“目标”字段中的下拉列表,Selenium IDE 在录制时成功捕获了元素的所有定位器,例如 id、name、css、xpath 等。
单击第 4 步,其 Command、Target 和 Value 字段的值分别为 type、id=name 和 Selenium。该元素的其他定位器值(如 name、css 等)在“目标”下拉列表中可见。

我们在应用程序上执行的所有步骤都将在 Selenium IDE 的 Command、Target 和 Value 字段中记录,右上角有一个 REC 按钮。
下面突出显示的图像部分表示文本脚本编辑器窗格。它包含在启用录制时以测试步骤形式记录的所有用户交互。

要在 Selenium IDE 中默认识别 Web 元素,请单击下面图像中突出显示的 “在页面中选择目标” 按钮。

单击 “在页面中选择目标” 按钮后,用户将在网页上获得 “选择元素” 按钮。选择元素后,其定位器值将由 Selenium IDE 填充。

所选 Web 元素的对应定位器值已在“目标”中生成。然后,如果单击 “在页面中查找目标” 按钮,则定位器值已填充的元素将在页面上突出显示。

请注意,“在页面中查找目标” 和 “在页面中选择目标” 按钮仅在 Command 字段中存在有效值时才启用,否则它们将保持禁用状态,如下面的图像中突出显示的那样。

定位器 ID
使用 id 定位器定位输入框,然后在 Selenium IDE 中输入文本 Selenium 的步骤如下所示:
步骤 1 - 执行存储和访问变量的步骤 1 到 4。
步骤 2 - 在 Command 字段中输入 open,在 Target 字段中输入Selenium 自动化实践表单,以启动应用程序。

步骤 3 - 在 Command 字段中输入 type,在 Target 字段中输入 id=firstname,在 Value 字段中输入 Selenium。请注意,id=firstname 是 id 定位器,其值为输入框的 firstname,Selenium 是要输入的值。

步骤 4 - 在 Command 字段中输入 close 以终止浏览器。

步骤 5 - 单击“运行所有测试”按钮。

在上面的示例中,我们使用 id 定位器在 First Name: 标签旁边的输入框中输入了文本 Selenium,该标签出现在日志下。此外,我们在顶部获得了绿色勾号,表示测试已通过。
定位器 Name
使用 name 定位器定位输入框,然后在 Selenium IDE 中输入文本 Selenium 的步骤如下所示:
步骤 1 - 单击上面示例中创建的第二步,并在 Command 字段中输入 type,在 Target 字段中输入 name=firstname,在 Value 字段中输入 Selenium。请注意,name=firstname 是 name 定位器,其值为输入框的 firstname,Selenium 是要输入的值。

步骤 2 - 单击“运行所有测试”,并等待测试执行完成。

在上面的示例中,我们使用 name 定位器在 First Name: 标签旁边的输入框中输入了文本 Selenium,该标签出现在日志下。
定位器 CSS
使用 css 定位器定位输入框,然后在 Selenium IDE 中输入文本 Selenium 的步骤如下所示:
步骤 1 - 单击上面示例中创建的第二步,并在 Command 字段中输入 type,在 Target 字段中输入 css=input#firstname,在 Value 字段中输入 Selenium。请注意,css=input#firstname 是 css 定位器,其值为输入框的 input#firstname,Selenium 是要输入的值。

步骤 2 - 单击“运行所有测试”。

在上面的示例中,我们使用 css 定位器在 First Name: 标签旁边的输入框中输入了文本 Selenium,该标签出现在日志下。
定位器 Xpath
使用 xpath 定位器定位输入框,然后在 Selenium IDE 中输入文本 Selenium 的步骤如下所示:
步骤 1 - 单击上面示例中创建的第二步,并在 Command 字段中输入 type,在 Target 字段中输入 xpath=//input[@id='firstname'],在 Value 字段中输入 Selenium。请注意,xpath=//input[@id='firstname'] 是 xpath 定位器,其值为输入框的 //input[@id='firstname'],Selenium 是要输入的值。

步骤 2 - 单击顶部的“运行所有测试”。

在上面的示例中,我们使用 xpath 定位器在 First Name: 标签旁边的输入框中输入了文本 Selenium,该标签出现在日志下。
结论
本教程全面介绍了 Selenium IDE 定位策略。我们从一个示例开始,逐步讲解了不同的定位策略,并演示了如何在 Selenium 中使用它们。这将使您深入了解 Selenium IDE 中的定位策略。建议您持续练习所学内容,并探索与 Selenium 相关的其他内容,以加深理解并拓宽视野。