4

Am using Eclipse, TestNG and 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?

vidit
  • 6,293
  • 3
  • 32
  • 50
user2356679
  • 371
  • 2
  • 5
  • 15
  • 2
    May I know why you don't want to use `isDisplayed`? – vidit May 07 '13 at 03:32
  • Its not that i dont want to use isDisplayed but how to use isDisplayed with findElements? In findElement, we can use it as driver.findElement.isDisplayed but in findElements, i dont see this method. Please help – user2356679 May 07 '13 at 22:21
  • Just a tip: you could change your XPath expression to just bring visible elements. For more, check: http://stackoverflow.com/questions/651783/how-do-i-select-only-visible-elements-using-xpath – acdcjunior May 11 '13 at 23:54
  • Thanks... i added the 'div[contains(@style,'display: none')]' in my xpath and it works. Nice one again... – user2356679 May 17 '13 at 04:48

2 Answers2

2

In C#, you can create WebDriver extension method like this:

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:

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.

// 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;
}
sjngm
  • 12,423
  • 14
  • 84
  • 114
Yi Zeng
  • 32,020
  • 13
  • 97
  • 125
1

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.

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'))]"));
Sameer Patil
  • 441
  • 3
  • 10