[英]Get selected option using Selenium WebDriver with Java
I'm new to Selenium WebDriver. 我是Selenium WebDriver的新手。 I'm testing a drop down list.
我正在测试一个下拉列表。 Here is the code I used to select an item from the drop down.
这是我用来从下拉菜单中选择项目的代码。
Select dropdown = new Select(driver.findElement(By.xpath("//select")));
dropdown.selectByValue("FEM");
This is working fine, but what I need is to get selected item as a text. 这工作正常,但是我需要的是将所选项目作为文本。 For an example, under the
value = FEM
, the text displays is female. 例如,在
value = FEM
,文本显示为女性。 I need to get the text as Selected value is female. 我需要获取文本,因为“选定值”是女性。
I've searched some articles and none of this worked. 我搜索了一些文章,但这些都没有。 Please help.
请帮忙。 :)
:)
Select
has getFirstSelectedOption()
method. Select
具有getFirstSelectedOption()
方法。 From there you can use getText()
从那里可以使用
getText()
Select dropdown = new Select(driver.findElement(By.xpath("//select")));
dropdown.selectByValue("FEM");
WebElement option = dropdown.getFirstSelectedOption();
String text = option.getText();
You can use element1.selectByVisibleText(value);
您可以使用
element1.selectByVisibleText(value);
if you want to set the option using text instead of value 如果要使用文本而不是值来设置选项
If you want to get the value use element1.getAllSelectedOptions().get(0).getText()
如果要获取值,请使用
element1.getAllSelectedOptions().get(0).getText()
or element1.getFirstSelectedOption()
或
element1.getFirstSelectedOption()
而不是使用dropdown.selectByvalue("FEM")
使用dropdown.selectByVisibleText("FEM")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.