问题
If we use the selenium code
Select sel = new Select(ele);
eles = sel.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.
Code:
<select id="userType" >
<option value="administrator">Administrator</option>
<option value="instructor">Instructor</option>
<option class="studenthide" value="student" style="display: none;">Student</option>
</select>
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?
回答1:
Here goes the alternative snippet:
List<WebElement> elements = driver.findElements(By.xpath(".//select/option[not(contains(@style,'display: none'))]"));
for(WebElement element : elements){
System.out.println(element.getText());
}
回答2:
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"))]"))
来源:https://stackoverflow.com/questions/46469179/how-to-omit-hidden-options-from-dropdown-using-selenium