Hroft
Hroft

Reputation: 4187

Get all visible elements on web page

I need to get all visible elements on web page to click on them, but it's too long to check each element with .displayed? Selenium method. Is there another way to create array with only visible elements to avoid checking.

Seems I need only non-grey elements from firebug.

//*[not(contains(@style,'display:none'))]

request doesn't solve my problem, because not all of invisible elements has such attribute.

Upvotes: 3

Views: 4364

Answers (2)

CBRRacer
CBRRacer

Reputation: 4659

If you are using C#.NET you can use a lambda expression to remove any elements that are not displayed. If not then this wouldn't work. This will get you every single anchor element, input element, and select element. Then it would remove any that were not displayed.

browserDriver.Navigate().GoToUrl("http://www.yahoo.com/");
List<IWebElement> theseElements = browserDriver.FindElements(By.TagName("a")).ToList();
theseElements.AddRange(browserDriver.FindElements(By.TagName("input")).ToList());
theseElements.AddRange(browserDriver.FindElements(By.TagName("select")).ToList());
theseElements.RemoveAll(i => !i.Displayed); //LAMBDA EXPRESSION
foreach (IWebElement element in theseElements)
{
    element.Click();
}

Upvotes: 2

Igbanam
Igbanam

Reputation: 6082

I know this isn't tagged but using the jQuery visible selector, it's as easy as :visible.

Upvotes: 0

Related Questions