4. Find Element Vs. FindElements in Selenium

— 3 min read

Introduction

Selenium has two methods to get elements from a webpage. Those two methods are findElement & findElements. We can use id, name, class name, tag name, and XPath as inputs to those two methods when retrieving elements. First, let’s see the difference between the two methods

findElements
Returns all the elements found on for the given parameter.
Returns null if there are no elements found for the given parameter.
Returned elements are indexed from 0 like an array.

findElement
Returns only one element and when multiple elements are found for the given parameter, it returns the first most available element only.
Returns NoSuchElementException error if no elements found for the given parameter.

In this article, we will see how to use these two methods. Learn-html.org is a website for learning different programming languages.  If you check the dropdown in the navigation menu, you can see HTML is unclickable as we currently on that page. 

In this article, we will demonstrate the findElement method to display the unclickable element and the findElements method to display all the programming languages in that navigation menu.

Writing the Code

Open the Eclipse IDE and create a new class. We will name our class as FindingElements

Next, import the necessary libraries that we need for this task.

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

As usual, our first step is to create a WebDriver element.

System.setProperty("webdriver.chrome.driver", "D:\\\\chromedriver\_win32\\\\chromedriver.exe"); 
WebDriver driver=new ChromeDriver();

Next, we will define the webpage URL that we need to get the elements from.

driver.get("https://www.learn-html.org/");

Next, we have to get the list of programming languages this site teaches. It is in the navigation menu. So let’s inspect it to find a way to get the using findElement and findElements methods. 

You can see, all the programming languages menu items share dropdown-item class name while HTML has a unique class name called disabled apart from dropdown-item. So we assume, class names are suitable to be used with the two methods here. 
To get the current course we are in (HTML), we will use the unique class name with findElement method. 

WebElement element = driver.findElement(By.className("disabled")); 

To get the full list, we will use dropdown-item class with findElements method. It’s important to remember findElements return a list of WebElements. 

List<WebElement> elements = driver.findElements(By.className("dropdown-item"));

Notice that these two methods only return the HTML Elements. To print programming language names, we have to get innerHTML from the elements. 

You can print HTML as follows. 

System.out.println("The Inactive Course (findElement method): " + element.getAttribute("innerHTML"));

To print all the programming languages, you have to loop through the list and then get the innerHTML of each element. You can do it as follows. 

for (int i=0; i<elements.size();i++){
	System.out.println(elements.get(i).getAttribute("innerHTML"));
}

package myPackage;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver; 
public class FindingElement {
public static void main(String\[\] args) { 
       		System.setProperty("webdriver.chrome.driver", "D:\\\\chromedriver\_win32\\\\chromedriver.exe"); 
		WebDriver driver=new ChromeDriver(); 
   		driver.get("https://www.learn-html.org/");
   		//findElement Demonstration
   		WebElement element = driver.findElement(By.className("disabled"));
   		System.out.println("The Inactive Course (findElement method): " + element.getAttribute("innerHTML"));
   		System.out.println();
   		//findElemets Demonstration
    		List<WebElement> elements = driver.findElements(By.className("dropdown-item"));
    		System.out.println("Number of elements in Dropdown Menu: " +elements.size());
    		System.out.println("All Courses (findElements method):");
	
		for (int i=0; i<elements.size();i++){
			System.out.println(elements.get(i).getAttribute("innerHTML"));
		}    	      
   	}
}

You can see a screenshot of the code and the output below. 

Thank you for joining us in this tutorial. We hope to see you in another article.