Selenium:从下拉菜单中获取当前值
我试图找到一个简单的 Selenium 调用来从选择下拉列表中获取当前选项。我知道有些调用会获取列表中的所有值,但我想知道当前选择了哪个选项。如果这是微不足道的,我很抱歉,但 google 和 Selenium IDE 没有帮助我。谢谢。
I'm trying to find a simple Selenium call to grab the current option from a select drop-down list. I'm aware there are calls which grab all the values in a list but I wish to know which option is currently selected. Apologies if this is trivial but google and Selenium IDE didn't help me. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该能够使用 getSelected* 命令返回所选项目的 ID、索引或标签。下面引用自 Selenium 参考:
storeSelectedId ( selectLocator, variableName )
获取指定选择元素中选定选项的选项元素 ID。
参数:
返回:指定选择下拉列表中选定的选项 ID
storeSelectedIndex ( selectLocator, variableName )
获取指定 select 元素中所选选项的选项索引(选项编号,从 0 开始)。
参数:
返回:指定选择下拉列表中选定的选项索引
storeSelectedLabel ( selectLocator, variableName )
获取指定选择元素中选定选项的选项标签(可见文本)。
参数:
返回:指定选择下拉列表中选定的选项标签
You should be able to use the getSelected* commands to return the ID, index, or label of the selected item. Below is quoted from the Selenium Reference:
storeSelectedId ( selectLocator, variableName )
Gets option element ID for selected option in the specified select element.
Arguments:
Returns: the selected option ID in the specified select drop-down
storeSelectedIndex ( selectLocator, variableName )
Gets option index (option number, starting at 0) for selected option in the specified select element.
Arguments:
Returns: the selected option index in the specified select drop-down
storeSelectedLabel ( selectLocator, variableName )
Gets option label (visible text) for selected option in the specified select element.
Arguments:
Returns: the selected option label in the specified select drop-down
我会使用
storeSelectedValue
或getSelectedValue
JUNIT
硒作用
I would use
storeSelectedValue
orgetSelectedValue
JUNIT
Selenium Action
有一个练习此类内容的链接:
“https://letskodeit.teachable.com/p/practice< /a>”
有一个“选择类别示例”
1.在此测试中,首先单击下拉菜单中的“本田”
2.然后提取选择标签作为选项标签“Honda”的父级
3.然后将其转换为Select对象
4.然后我使用 getFirstSelectedOption() 将其与预期值“本田”进行比较。
如果您需要下面的整个测试类注释
there is a link to practice such things:
"https://letskodeit.teachable.com/p/practice"
there's a "Select Class Example"
1.in this test first it clicks on "Honda" in the dropdown menu
2. then extracts the select-tag as parent of the option-tag "Honda"
3. then converts it to Select object
4. then i uses the getFirstSelectedOption() to compare it with the expected value "Honda.
if you need the whole Test class comment below