How to omit hidden options from Dropdown using selenium

流过昼夜 提交于 2019-12-08 11:44:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!