如何在 Selenium 2 中选择/获取下拉选项 [英] How to select/get drop down option in Selenium 2

查看:48
本文介绍了如何在 Selenium 2 中选择/获取下拉选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的 selenium 1 代码转换为 selenium 2,但找不到任何简单的方法来在下拉菜单中选择标签或获取下拉菜单的选定值.你知道如何在 Selenium 2 中做到这一点吗?

I am converting my selenium 1 code to selenium 2 and can't find any easy way to select a label in a drop down menu or get the selected value of a drop down. Do you know how to do that in Selenium 2?

这里有两个语句在 Selenium 1 中有效,但在 2 中无效:

Here are two statements that work in Selenium 1 but not in 2:

browser.select("//path_to_drop_down", "Value1");
browser.getSelectedValue("//path_to_drop_down");

推荐答案

查看关于 填写表单 使用 selenium 文档中的 webdriver 和 选择 类.

Take a look at the section about filling in forms using webdriver in the selenium documentation and the javadoc for the Select class.

根据标签选择一个选项:

To select an option based on the label:

Select select = new Select(driver.findElement(By.xpath("//path_to_drop_down")));
select.deselectAll();
select.selectByVisibleText("Value1");

获取第一个选定的值:

WebElement option = select.getFirstSelectedOption()

这篇关于如何在 Selenium 2 中选择/获取下拉选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆