如何使用硒从下拉列表中省略隐藏选项 [英] How to omit hidden options from Dropdown using selenium
问题描述
如果我们使用硒代码
Select sel = new Select(ele);
eles = sel.getOptions();
它将返回下拉选项的所有元素,我们可以获得值. 但是,如果某些下拉菜单选项处于隐藏状态,并且我们使用相同的Select.getOptions(),它将返回所有选项,包括隐藏选项.
it will returns all element of dropdown options and we can get the value. But if some options of dropdown are in hidden state and if we use the same Select.getOptions(), it will return the all options including the hidden options.
代码:
<select id="userType" >
<option value="administrator">Administrator</option>
<option value="instructor">Instructor</option>
<option class="studenthide" value="student" style="display: none;">Student</option>
</select>
从About代码中,管理员"和讲师"仅显示在下拉列表中,而学生"没有显示在下拉列表中.
From the about code, Administrator and Instructor only displayed in dropdown, but Student is not displayed in dropdown.
那么我们如何才能从下拉列表中仅获取显示的选项?
So how can we get only the displayed options from the drop-down?
推荐答案
我不确定如何使用选择类.但是您可以使用xpath下方的列表将所有可见元素放入列表中
I am not sure how to get using select class. But you can get all the visible elements into a list using below xpath
driver.findElements(by.xpath("//select[@id="userType"]/option[not(contains(@style,"display: none"))]"))
这篇关于如何使用硒从下拉列表中省略隐藏选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!