Difference Between FindElement And FindElements Methods

FindElement And FindElements Methods In Selenium: In our previous post, we are trying to learn about the different Selenium WebDriver methods and commands. We have different types of elements on a web page. So, to interact with the web page elements, we need to use the web element commands/actions. So, to interact with those elements, we have to locate the element, and we can do that with the help of the WebDriver instance.

Why do you need to FindElement And FindElements Methods?

To interact with the web page elements, we need to locate them first. So, we can use the find element command to identify only one element from the web page. We can use the find elements commands if we want to find multiple elements from the web page. We can find the elements within the web page with the help of ID, Name, Class Name, Link Text, Partial Link Text, Tag Name, and Xpath.

FindElement And FindElements Methods With Example
FindElement And FindElements Methods With Example

How to Locate Element Using findElement?

We use the findElement method to locate the element, which takes the By object as the parameter and returns an object type of WebElement. With the By object, we can use the various locator strategies like ID, Name, Class Name, XPath, etc. The Syntax of FindElement looks something like this:

FindElement Syntax:

WebElement elementName = driver.findElement(By.LocatorStrategy("LocatorValue"));

The Locator value should be unique to identify the elements uniquely. Developers or testers are responsible for generating the locator value so that it uniquely identifies the elements with the help of any locator strategies.

package com.selenium.practice.basic;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class FindElement_Example 
{
   public static void main(String[] args) throws InterruptedException 
   {
      WebDriver driver;
      System.setProperty("webdriver.chrome.driver", "Path Of Browser driver File");

      driver = new ChromeDriver();
      driver.get("https://orangehrm-demo-6x.orangehrmlive.com");
      driver.manage().timeouts().implicitlyWait(5000, TimeUnit.SECONDS);
      
      //Clearing The UserName Field Value
      driver.findElement(By.id("txtUsername")).clear();
      Thread.sleep(5000);
      
      driver.close();
   }
}

How to Locate Element Using findElements?

As we have seen, the find element method takes the by the object and returns a web element, and FindElements also takes the By object and returns a list of web elements. If FindElements cannot locate elements, it returns an empty list. the syntax for FindElements looks like this:

FindElements Syntax:

List<WebElement> elementName = driver.findElements(By.LocatorStrategy("LocatorValue"));
package com.selenium.practice.basic;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class FindElements_Example 
{

   public static void main(String[] args) throws InterruptedException 
   {
      WebDriver driver;
      System.setProperty("webdriver.chrome.driver", "Path Of Browser driver file");

      driver = new ChromeDriver();
      driver.get("https://testpages.herokuapp.com/basic_html_form.html");
      driver.manage().timeouts().implicitlyWait(5000, TimeUnit.SECONDS);
      driver.manage().window().maximize();
      
      //Clearing The UserName Field Value
      List<WebElement> li=driver.findElements(By.name("radioval"));
      Thread.sleep(5000);
      
      //Selecting the 1st element
      li.get(0).click();
      Thread.sleep(5000);
      
      driver.close();
   }
}

Difference Between findElement & findElements

FindElementFindElements
In the Findelement, we can find the first element within the current page using the “locating mechanism.”With findElements, we can find all the matching elements within the current page using the “locating mechanism.”
Find element returns the First element or Single elementIt will return a list of web elements.
If It cannot locate the element, then it will go through NoSuchElementException.If It cannot locate the element, it will go through NoSuchElementException.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment