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
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 🙂
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.
Mukesh Otwani says
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
Hari says
What is the difference between CLICK() & Submit()?
Mukesh Otwani says
Hi Hari,
Please refer this link https://seleniumhq.github.io/selenium/docs/api/java/index.html
Hari says
Difference between webdriver and webelement? Can you explain with examples?
Mukesh Otwani says
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
Ramsai says
Thank you very much for this blog..well explained…Really useful stuff for people like me who want to learn automation testing
Mukesh Otwani says
Thank Ramsai, Please share with your friends as well.Keep visting.