• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Advance Selenium / Difference Between findelement and findelements in Selenium Webdriver

Difference Between findelement and findelements in Selenium Webdriver

March 18, 2015 by Mukesh Otwani 9 Comments

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 🙂

Filed Under: Advance Selenium, Basic Selenium Tagged With: Selenium Tips and Trick

Reader Interactions

Comments

  1. Meghana R says

    April 29, 2021 at 7:01 PM

    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.

    Reply
    • Mukesh Otwani says

      April 30, 2021 at 8:02 PM

      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

      Reply
  2. Hari says

    April 17, 2019 at 10:02 AM

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

    Reply
    • Mukesh Otwani says

      April 17, 2019 at 2:38 PM

      Hi Hari,

      Please refer this link https://seleniumhq.github.io/selenium/docs/api/java/index.html

      Reply
  3. Hari says

    April 17, 2019 at 9:27 AM

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

    Reply
    • Mukesh Otwani says

      April 17, 2019 at 1:31 PM

      Hi Hari,

      Please refer these links
      1. https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebDriver.html
      2. https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebElement.html

      Reply
  4. Ramsai says

    March 24, 2016 at 7:09 AM

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

    Reply
    • Mukesh Otwani says

      March 26, 2016 at 1:55 PM

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

      Reply

Leave a Reply Cancel 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.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?