Advance Selenium, Basic Selenium

Difference Between findelement and findelements in Selenium Webdriver

Hello Welcome to Selenium Tutorials, In this post, we will see what is the difference between FindElement and findElements in Selenium.

 

Difference Between findelement and findelements in Selenium Webdriver

 

Difference Between findelement and findelements in Selenium Webdriver

findElement () will return only single WebElement and if that element is not located or we use some wrong selector then it will throw NoSuchElement exception.

findElements() will return List of WebElements – for this we need to give locator in such a way that it can find multiple elements and will return you list of web elements then using List we can iterate and perform our operation.

We will discuss through the basic program  and will use http://jqueryui.com/ for our sample application.

 

Program 1- for findElement

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class findElementDemo {

 public static void main(String[] args) throws Exception {
  
 // Open browser
  WebDriver driver=new FirefoxDriver();
  

// maximize the window

driver.manage().window().maximize();

 // open application

driver.get("http://jqueryui.com");

   // Wait for 5 seconds

Thread.sleep(5000);
  
  // Here xpath will select unique Element
   WebElement demos=driver.findElement(By.xpath("//*[@id='menu-top']/li[1]/a"));


  // click on link
  demos.click();
  
 }

}

Program 2- for findElements()

import java.util.Iterator;
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 findElementDemo {

 public static void main(String[] args) throws Exception {
  
    // load browser
        WebDriver driver=new FirefoxDriver();


  // maximize window
  driver.manage().window().maximize();
 

 // pass url
  driver.get("http://jqueryui.com");
  

// Wait for 5 seconds
  Thread.sleep(5000);
  
  // Here below xpath will select 8 Element
  
 List<WebElement> links=driver.findElements(By.xpath(".//*[@id='menu-top']/li"));
 
 // print the total number of elements
  System.out.println("Total element is "+links.size());


 // Now using Iterator we will iterate all elements
Iterator<WebElement> i1=links.iterator();
    

// this will check whether list has some element or not
while(i1.hasNext()){
    

   // Iterate one by one
    WebElement ele1=i1.next();
    

    // get the text
    String name=ele1.getText();
    

   // print the text
    System.out.println("Elements name is "+name);
     
    }
  
 }


}

 

If you still have some query then comment in below section.

Thanks for visiting my blog. Keep in touch.

Have a nice day 🙂

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

9 thoughts on “Difference Between findelement and findelements in Selenium Webdriver

  1. Meghana R says:

    Hi Mukesh,

    I have a small doubt. To find, find elements do I need to locate all the elements? For example, I wanted to find a list of all candidate names. So, how I need to do.

    1. Hi Meghana,

      If you want to get all candidates names in one shot, then locator property which points to all required texts in the Webpage using findElemets only. This will return list of those webelemets. Do getText() to get required text using the loop mechanism. If you want only one name then get a particular locator

  2. Hari says:

    What is the difference between CLICK() & Submit()?

  3. Hari says:

    Difference between webdriver and webelement? Can you explain with examples?

  4. Ramsai says:

    Thank you very much for this blog..well explained…Really useful stuff for people like me who want to learn automation testing

    1. Thank Ramsai, Please share with your friends as well.Keep visting.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.