In this tutorial we will learn find Elements example in selenium web driver
findElements in selenium will return the list of web elements if match found for all the elements for a given selector. If no match found it will return NULL unlike findElement will return element not found exception.
So below example will show you how to use findElements and how to loop through the list of web elements .
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class CountOfLinks {
public static void main(String[] args) {
String path ="F:\\Automation_lib\\chromedriver_win32\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver",path);
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://www.icicibank.com/");
List<WebElement> els =driver.findElements(By.tagName("a"));
System.out.println("Number of links :"+els.size());
for(WebElement e :els) {
System.out.println(e.getAttribute("href")+"*****"+e.getText());
}
}
}