Loop through WebElements using Java 8

0 votes

It’s quite common to have a list of WebElements where we want to iterate over. We can use the Java 8 Stream API feature to easily iterate over collections.


final List<String> productNames = driver
.findElements(By.cssSelector("ul.product_list a.product-name")).stream()
.map(WebElement::getText).collect(Collectors.toList());

Please follow and like us:
Follow by Email
Facebook
Google+
http://www.seleniuminaction.com/loop-through-webelements-using-java-8/
Twitter
LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *