如何找到下拉菜单的选定选项? [英] How to find the selected option of a Dropdown?
本文介绍了如何找到下拉菜单的选定选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们如何在Selenium Webdriver中找到下拉菜单的选定选项?
How do we find the selected option of a Dropdown in Selenium Webdriver??
我尝试-
WebElement element = driver.findElement(By.xpath(locator1));
Select select = new Select(element);
List<WebElement> SO = select.getAllSelectedOptions();
String S = SO.toString();
System.out.println(S);
返回类似于-的WebElement
which returns WebElements like -
[[[[[FirefoxDriver: firefox on XP (c388e8a8-09d5-41b9-b086-0278c639d8b1)] -> xpath: .//*[@id='city']]] -> tag name: option]]
我要找到所选的选项吗?
I want to find the option that is selected?
推荐答案
您可以在Select
对象上使用getFirstSelectedOption()
方法来解析所选元素,并可以通过getText()
方法获取其文本.参见下面的示例:
You can use the getFirstSelectedOption()
method on your Select
object to resolve the selected element and you can get it's text by the getText()
method. See example below:
对于单个选择:
WebElement element = driver.findElement(By.xpath(locator1));
Select select = new Select(element);
WebElement selectedOption = select.getFirstSelectedOption();
System.out.println(selectedOption.getText());
进行多项选择:
WebElement element = driver.findElement(By.xpath(locator1));
Select select = new Select(element);
List<WebElement> selectedOptions = select.getAllSelectedOptions();
for(WebElement selectedOption : selectedOptions) {
System.out.println(selectedOption.getText());
}
这篇关于如何找到下拉菜单的选定选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文