如何使用selenium从Dropdown中省略隐藏选项

如果我们使用selenium代码

Select sel = new Select(ele); eles = sel.getOptions(); 

它将返回下拉选项的所有元素,我们可以获取值。 但是如果下拉列表的某些选项处于隐藏状态,并且如果我们使用相同的Select.getOptions(),它将返回包括隐藏选项在内的所有选项。

码:

   Administrator Instructor Student  

从about代码中,Administrator和Instructor仅显示在下拉列表中,但Student不会显示在下拉列表中。

那么我们如何只从下拉列表中获取显示的选项呢?

这里是替代片段:

 List elements = driver.findElements(By.xpath(".//select/option[not(contains(@style,'display: none'))]")); for(WebElement element : elements){ System.out.println(element.getText()); } 

我不知道如何使用select class。 但是您可以使用xpath下面的所有可见元素到列表中

  driver.findElements(by.xpath("//select[@id="userType"]/option[not(contains(@style,"display: none"))]"))