Identifying the web elements in a page is required to interact with the web page. Selenium WebDriver give us FindElement and FindElements methods to locate elements on the webpage. There are multiple ways to identify uniquely a web element within the web page such as ID, Name, Class Name, LinkText, PartialLinkText, TagName, and XPath.

FindElement Command
This method locates the first web element on the current web page matching the criteria mentioned as parameters.
If the web element is not found, it will throw an exception – NoSuchElementException.
Syntax:
findElement(By arg0):WebElement - WebDriver
Command:
driver.findElement(By.xpath("Xpath location"));
How to use FindElement in Selenium
The following application is used for demo purposes:
Scenario – Valid
- Open the web page
- Close the child window and move to facebook main page
- Enter the emailId and password and click on Log In Button
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FindElement_Example {
public static voidmain(String[] args){
System.setProperty("webdriver.gecko.driver","C:\\Users\\vibha\\Downloads\\geckodriver-v0.24.0-win64\\geckodriver.exe");
// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();
// Set implicit wait of 60 sec for managing waits in selenium webdriver
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
// Launch a new web page and load Facebook
driver.get("https://www.facebook.com/");
Thread.sleep(1000);
//switch from main window to child window
for (String handle1 : driver.getWindowHandles()) {
driver.switchTo().window(handle1);
}
driver.findElement(By.id("u_0_k")).click();
// Enter the values in Email and Password Textboxes
driver.findElement(By.name("email")).sendKeys("abc@gmail.com");
driver.findElement(By.id("pass")).sendKeys("abcd1234!");
// Click on Log In Button
driver.findElement(By.id("u_0_b")).click();
driver.close();
}
}
Scenario – Error
- Open the web page
- Close the child window and move to facebook main page
- Use incorrect locator to find emailId
- Execution halts and then stopped with NoSuchElementException
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FindElement_Example {
public static void main(String[] args){
System.setProperty("webdriver.gecko.driver","C:\\Users\\vibha\\Downloads\\geckodriver-v0.24.0-win64\\geckodriver.exe");
// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();
// Set implicit wait of 60 sec for managing waits in selenium webdriver
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
// Launch a new web page and load Facebook
driver.get("https://www.facebook.com/");
Thread.sleep(1000);
//switch from main window to child window
for (String handle1 : driver.getWindowHandles()) {
driver.switchTo().window(handle1);
}
driver.findElement(By.id("u_0_k")).click();
// Enter the values in Email and Password Textboxes
driver.findElement(By.name("email")).sendKeys("abc@gmail.com");
driver.findElement(By.id("pass")).sendKeys("abcd1234!");
// Click on Log In Button
driver.findElement(By.id("u_0_b")).click();
driver.close();
}
}

FindElements Command
- This method locates all the web elements on the current web page matching the criteria mentioned as parameter.
- If not found any WebElement on current page as per given element locator mechanism, it will return empty list.
Syntax
findElements (By arg0):List
Below is the example of findElements
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FindElements_Example {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","C:\\Users\\vibha\\Downloads\\geckodriver-v0.24.0-win64\\geckodriver.exe");
// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();
// Set implicit wait of 60 sec for managing waits in selenium webdriver
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
// Launch a new web page and load Facebook
driver.get("https://www.facebook.com/");
Thread.sleep(1000);
// switch from main window to child window
for (String handle1 : driver.getWindowHandles()) {
driver.switchTo().window(handle1);
}
driver.findElement(By.id("u_0_k")).click();
driver.findElement(By.xpath("//*[@id='u_0_2']")).click();
// Find all elements and store into list
List Link_List = driver.findElements(By.id("month"));
for (WebElement link : Link_List)
// Print all the elements from the list
System.out.println(link.getText());
driver.close();
}
}
Output
Month
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec
Differences between findElement() and findElements() method:
· Return type of findElement() is a WebElement (single) while Return type of findElements() is a List (multiple).
· findElement() method will throw noSuchElementException if web element is not found in WebPage while findElements() will return an empty list and do not throw any.
We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!