Difference between findElement and findElements in selenium WebDriver.

What is the difference between findElement() and findElements() in selenium WebDriver?

driver.findElement() and driver.findElements() both methods are used to find (locate) the WebElements. But as name suggest, they are different. Let us see the difference between them.
driver.findElement(): – 
 
            1.      Return single WebElement from the webpage.
         2.   If multiple WebElements with same location are present, then this returns first matching WebElement with specified locator.
            3.      If element is not found on the webpage, then it throws NoSuchElementFoundexception.
            4.      We can use WebElement methods on returned element again
Example – 

WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://testing.webield.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.findElement(By.xpath("html/body/div[1]/div/div[2]/form/div[1]/div/input")).sendKeys("admin");

 driver.findElements(): –        
               
                  1.       Returns all (array of) matching elements with specified locator.
                  2.      findElements() is used to find the group of elements in to the list.
                  3.      This method returns List<WebElement>. That is the List of the WebElements.
                  4.      If no matching element is found, this method will return an empty List.
                 5.      As this method returns a list of WebElements, we have to get individual WebElement using for loop / iterator.
Example –

List<WebElement> l = element1.findElements(By.tagName("td")); 
System.out.println("Total elements present in table are: " +l.size());

for (int i = 0; i<l.size(); i++){
System.out.println("table contents: "+l.get(i).getText());

}

Hope this Helps!!!!

Leave Comment

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

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories