如何使用Selenium WebDriver和Java获取选定的选项 [英] How to get selected option using Selenium WebDriver with Java
本文介绍了如何使用Selenium WebDriver和Java获取选定的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Selenium的新手,我被困在这里:
I am newbie in Selenium and i am stuck here :
我想获取所选标签或的值使用Selenium WebDriver下拉,然后打印 控制台。
我可以选择下拉列表中的任何值,但我无法检索所选值并打印它。
I am able to select any value from drop down but I am not able to retrieve the selected value and print it.
Select select = new
Select(driver.findElement(By.id("MyDropDown"))).selectByVisibleText(data[11].substring(1 , data[11].length()-1));
WebElement option = select.getFirstSelectedOption();
但我所有的努力都是徒劳的
任何帮助都将不胜感激。在此先感谢:)
But all my efforts going in vain Any help would be appreciated . Thanks in advance :)
推荐答案
您应该能够使用 getText()获取文本
(对于使用 getFirstSelectedOption()
的选项元素):
You should be able to get the text using getText()
(for the option element you got using getFirstSelectedOption()
):
Select select = new Select(driver.findElement(By.xpath("//select")));
WebElement option = select.getFirstSelectedOption();
String defaultItem = option.getText();
System.out.println(defaultItem );
这篇关于如何使用Selenium WebDriver和Java获取选定的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文