When to use findElement() and findElements() ?

findElement() :
findElement() is used to find the first element in the current web page matching to the specified locator value. In this case of findElement() we will get only the first matching element .
Syntax code :
WebElement element = driver.findElement(By.xpath(“//div[@id=’example’]//ul//li”));

findElements():
findElements() is used to find all the elements in the current web page matching to the specified locator value. In this case all the matching elements would be fetched and stored in the list of WebElements.
Syntax:
List elementList = driver.findElements(By.xpath(“//div[@id=’example’]//ul//li”));

Leave a Reply