如何使用Selenium从选择下拉列表中检索选项的值? [英] How to retrieve the value of options from a select drop-down using Selenium?

查看:184
本文介绍了如何使用Selenium从选择下拉列表中检索选项的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在给定的代码中,我需要选择下拉列表中每个选项的值"属性值列表:

In the given code, I need the list of the value of the "value" attribute for each of the options in the select drop-down :

<select id="myselect">
<option value="val1">Some Text</option>
<option value="val2">Some Text</option>
<option value="val3">Some Text</option>
<option value="val4">Some Text</option>
<option value="val5">Some Text</option>
<option value="val6">Some Text</option>
</select>

我想要所有值的列表,这是 {val1,val2,val3,val4,val5,val6}

I want the list of all the values, which is {val1,val2,val3,val4,val5,val6}

推荐答案

要创建 属性的所有值的列表对于<select>标记中的每个选项,您可以使用以下基于 Java 的解决方案:

To create a List of all the values of the value attribute for each of the options with in the <select> tag you can use the following Java based solution:

Select dropDownSelect = new Select(driver.findElement(By.id("myselect")));
List<WebElement> dropDownActualValues = dropDownSelect.getOptions();
for(WebElement element:dropDownActualValues)
    System.out.println(element.getAttribute("value"));

这篇关于如何使用Selenium从选择下拉列表中检索选项的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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