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:

 

Example:  Find element and Click on Home button. 

 

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:

 

Example:  Store all records into list and get the size of all records and store into integer.

 

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