How to get selected option using Selenium WebDriver with Java(如何使用 Selenium WebDriver 和 Java 获得选定的选项)
问题描述
我想使用 Selenium WebDriver获取所选标签或下拉列表的值,然后在 控制台上打印.
I want to get the selected label or value of a drop down using Selenium WebDriver and then print it on the console.
我可以从下拉列表中选择任何值,但我无法检索并打印所选值:
I am able to select any value from the 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 were in vain. How do I get the selected option?
推荐答案
你应该能够使用 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 获得选定的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Selenium WebDriver 和 Java 获得选定的选项
基础教程推荐
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 存储 20 位数字的数据类型 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- Struts2 URL 无法访问 2022-01-01
