findElement and findElements in Selenium :
findElement():
- Every time when we need to perform any operation on WebElement we used findElement method .
- Using findElement method we find WebElement from WebPage and perform Operations on it.
- findElement method returns only first / Single WebElement.
- If findElement does not found any WebElement on page then It throws a NoSuchElementException exception when it fails to find If the element.
- Go through below examples of findElement and findElements in Selenium.
Syntax:
1 | driver.findElement(By.xpath("Xpath location")); |
Example: Find element and Click on Home button.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class FindElement { public static void main(String[] args) { // Open browser WebDriver driver = new FirefoxDriver(); // Open Application driver.get("https://www.stqatools.com"); // Click on Home button driver.findElement(By.id("Home")).click(); } } |
findElements():
- When we want to fetch multiple WebElement then we use findElements for eg: Fetch all DropDown list records to verify record is present or not in DropDown list.
- For this, the list of WebElements will be returned, we have to give the locator in such a manner that it can search for many elements and return the list of web elements through the list, we can iterate it and do our operation Are there.
- If not found any WebElement on current page as per given element locator mechanism, it will return empty list.
Syntax:
1 | List link = driver.findElements(By.xpath("Xpath location")); |
Example: Store all records into list and get the size of all records and store into integer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class FindElements { public static void main(String[] args) { // Open browser WebDriver driver = new FirefoxDriver(); // Open application driver.get("https://www.stqatoolsl.com"); // Find all elements and store into list List allElement=driver.findElements(By.xpath("//td")); // Get the size of allElements and store into integer. int size = allElement.size(); // Print size of Elements System.out.println(size); } } |
Difference between findElement() and findElements() method:
- Return type of findElement() is a WebElement (single) while Return type of findElements() is a List<WebElement> (multiple).
- findElement() method will throw noSuchElementException if web element is not found in WebPage while findElement() will not throw any exception.
- Both return an empty List<WebElement> if not found in WebPage.
General among findElement() and findElements() methods:
- We use “Explicit wait” to handle WebElements using findElement.
- Both methods use the same strategy to find web elements , just they return it in different ways.
FindElement and FindElements Command Method Selenium and Difference Between FindElement And FindElements Methods and findElement and findElements Commands and Examples