java Selenium WebDriver- FindElements 返回未显示的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16410720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium WebDriver- FindElements returns elements which are not displayed
提问by user2356679
Am using Eclipse, TestNG and Selenium 2.32.
我正在使用 Eclipse、TestNG 和 Selenium 2.32。
List <<f>WebElement> elementOption = driver.findElements(By.xpath("//li[@role='option']"));
The code driver.findElements(By.xpath("//li[@role='option']"));
returns all the elements which are not displayed as well. The above 'elementOption' now contains all the elements, even the elements that are not displayed in the webpage. We can use IsDisplayed
along with findElement
method which will return only the element which is displayed in the webpage. Is there anything similar to IsDisplayed
that can be used with findElements
which will return only the elements that are displayed?
该代码driver.findElements(By.xpath("//li[@role='option']"));
返回所有未显示的元素。上面的“elementOption”现在包含所有元素,甚至是网页中未显示的元素。我们可以使用IsDisplayed
withfindElement
方法,该方法将仅返回网页中显示的元素。有没有类似的东西IsDisplayed
可以用于findElements
只返回显示的元素?
采纳答案by Yi Zeng
In C#, you can create WebDriver extension method like this:
在 C# 中,您可以像这样创建 WebDriver 扩展方法:
public static IList<IWebElement> FindDisplayedElements<T>(this T searchContext, By locator) where T : ISearchContext {
IList<IWebElement> elements = searchContext.FindElements(locator);
return elements.Where(e => e.Displayed).ToList();
}
// Usage: driver.FindDisplayedElements(By.xpath("//li[@role='option']"));
Or use Linq when you call FindElements
:
或者在调用时使用 Linq FindElements
:
IList<IWebElement> allOptions = driver.FindElements(By.xpath("//li[@role='option']")).Where(e => e.Displayed).ToList();
However, I am aware of that extension methods and Linq don't exist in Java. So you probably need to create you own static method/class using the same logic.
但是,我知道 Java 中不存在扩展方法和 Linq。因此,您可能需要使用相同的逻辑创建自己的静态方法/类。
// pseudo Java code with the same logic
public static List<WebElement> findDisplayedElements(WebDriver driver, By locator) {
List <WebElement> elementOptions = driver.findElements(locator);
List <WebElement> displayedOptions = new List<WebElement>();
for (WebElement option : elementOptions) {
if (option.isDisplayed()) {
displayedOptions.add(option);
}
}
return displayedOptions;
}
回答by Sameer Patil
If the elements which you are trying to retrieve contains attributes like style having display values, then you might need to just change your XPATH to get only displayed elements.
如果您尝试检索的元素包含具有显示值的样式等属性,那么您可能只需要更改 XPATH 以仅获取显示的元素。
List <WebElement> elementOption = driver.findElements(By.xpath("//li[@role='option'][contains(@style,'display: block;')]"));
or
或者
List <WebElement> elementOption = driver.findElements(By.xpath("//li[@role='option'][not(contains(@style,'display: none'))]"));