The difference between findElement and findElements in Selenium is how they behave when an element is not found.
findElementthrows aNoSuchElementExceptionif the specified element is not found on the page.findElementsdoes not throw an exception. Instead, it returns an empty list when no matching elements are found.
Whenever we want the automation script to continue executing even if an element is not present in the application, we can use findElements.
Example code:
WebDriver driver = new ChromeDriver();
List<WebElement> links = driver.findElements(By.tagName(“a”));
if(links.size() > 0){
//write automation script
}
Note:
If no <a> (anchor) tags are found on the page, the links list will be empty, but the script will continue running without throwing an exception.
Vijaya bhaskar Alladi Changed status to publish