Here you will learn the difference between findElement() and findElements() function of Webdriver (Selenium).
findElement:
1. findElement is use to locate single element and it return WebElement object of first occurrences element on web page.
2. If element not found it will throw exception NoSuchElementException
3. Syntax: findElement(By by).
Example:
WebElement element = driver.findElement(By.id("Home"));
element.click();
findElements:
1. findElements is use to find multiple element on webpage, exp: as we need to count total number of row in table
2. It return List of WebElement object of all occurrences of element.
3. If element not found it will return empty List of WebElement object.
4. Syntax: List element = findElenets(By by)
Example:
List {WebElement} element = driver.findElement(By.xpath("//table/tr"));
int size = element.size();
findElement:
1. findElement is use to locate single element and it return WebElement object of first occurrences element on web page.
2. If element not found it will throw exception NoSuchElementException
3. Syntax: findElement(By by).
Example:
WebElement element = driver.findElement(By.id("Home"));
element.click();
findElements:
1. findElements is use to find multiple element on webpage, exp: as we need to count total number of row in table
2. It return List of WebElement object of all occurrences of element.
3. If element not found it will return empty List of WebElement object.
4. Syntax: List
Example:
List {WebElement} element = driver.findElement(By.xpath("//table/tr"));
int size = element.size();