In C #, you can create a WebDriver extension method as follows:
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(); }
Or use Linq when you call FindElements :
IList<IWebElement> allOptions = driver.FindElements(By.xpath("//li[@role='option']")).Where(e => e.Displayed).ToList();
However, I know that extension and Linq methods do not exist in Java. Therefore, you probably need to create your own static method / class using the same logic.
source share