Selenium: How to select an option from a select menu?(Selenium:如何从选择菜单中选择一个选项?)
问题描述
我正在使用 PHPUnit Selenium 扩展在 PHP 中编写 Selenium 测试.
I am writing a Selenium test in PHP using the PHPUnit Selenium extension.
我知道如何在文本字段中输入内容:
I know how to type something into a text field:
$this->type('fieldName', 'value');
但是如何从下拉菜单中选择一个选项?
But how do I select an option from a drop-down menu?
推荐答案
要扩展其他(准确)答案,您可以根据选项的标签、值、id 或索引进行选择.来自 http://release.seleniumhq.org/selenium-core 的官方参考资料/1.0/reference.html:
To expand on the other (accurate) answers, you can select based on the label, value, id, or index of the options. From the official reference available at http://release.seleniumhq.org/selenium-core/1.0/reference.html:
select(selectLocator, optionLocator)
参数:
- selectLocator - 标识下拉菜单的元素定位器
- optionLocator - 选项定位器(默认为标签)
使用选项定位器从下拉列表中选择一个选项.
Select an option from a drop-down using an option locator.
选项定位器提供了指定 HTML Select 元素选项的不同方式(例如,用于选择特定选项,或用于断言所选选项满足规范).选择选项定位器有多种形式.
Option locators provide different ways of specifying options of an HTML Select element (e.g. for selecting a specific option, or for asserting that the selected option satisfies a specification). There are several forms of Select Option Locator.
- label=labelPattern:根据标签(即可见文本)匹配选项.(这是默认设置.)
- label=regexp:^[Oo]其他
- label=labelPattern: matches options based on their labels, i.e. the visible text. (This is the default.)
- label=regexp:^[Oo]ther
- 值=其他
- id=option1
- 索引=2
如果没有提供选项定位器前缀,则默认行为是匹配标签.
If no option locator prefix is provided, the default behaviour is to match on label.
这篇关于Selenium:如何从选择菜单中选择一个选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Selenium:如何从选择菜单中选择一个选项?


基础教程推荐
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何替换eregi() 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01