In c# i always use
`IList<IWebElement> SearchResult1 = Setup.driver.FindElements(By.CssSelector("div.comma.line"));`
and i access the SearchResult1
by SearchResult1[0] or SearchResult1 [].
It allow me to click on the SearchResult1[0]
too.
But how am i going to do this in Java. I have this list and cant access the list by its index number.
List<WebElement> matches = TopLayer.findElements(By.tagName("li"));
matches[0]
Im getting an error.
2 Answers 2
For list iterator use below loop
for ( WebElement we: matches ) {
//Do something
}
or you can: matches.get(0);
or matches.get(1);
and so on
Array can be access by its index like matches[0], matches[1]
while List is a ordered collection in Java. To access or add elements in list by using matches.get(index)
or matches.add(value)
methods.
Refer how to use list in Java