Difference Between FindElement And FindElements Methods

Difference Between FindElement And FindElements Methods

To automate a web application using Selenium WebDriver, we need to locate the elements on a web page. We use different locators depends on our needs to find the elements. Here is a link to the post “ Locators in Selenium “. In this post, we discuss two methods to find the elements on a webpage namely findElement and findElements methods.

Let’s see the detailed overview – difference between findElement and findElements methods.

The difference between findElement and findElements methods:

FINDELEMENT() METHOD:

findElement method is used to access a single web element on a page. It returns the first matching element. It throws a NoSuchElementException exception when it fails to find If the element.

Syntax:

1
driver.findElement(By.xpath(“Value of Xpath”));

FINDELEMENTS() METHOD:

findElements method returns the list of all matching elements. The findElement method throws a NoSuchElementException exception when the element is not available on the page. Whereas, the findElements method returns  an empty list when the element is not available or doesn’t exist on the page. It doesn’t throw NoSuchElementException.

Syntax:

1
List link = driver.findElements(By.xpath(“Value of Xpath”));

Let’s see a practical example – FindElement and FindElements methods in Selenium WebDriver:

FindElement method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package seleniumTutorial;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FindelementFindElements {
public static void main (String [] args){
// Open browser
WebDriver driver = new FirefoxDriver();
// To maximize the window
driver.manage.window.maximize();
// Open Application
driver.get(“https://www.google.co.in/?gws_rd=ssl#q=softwaretestingmaterial.com”);
// Get text of a particular link
String FindElement = driver.findElement(By.xpath(“//*[@id=’rso’]/div[1]/div/div/h3/a”)).getText();
// Print the value of the link
System.out.println(FindElement);
// Click on the link
driver.findElement(By.xpath(“//*[@id=’rso’]/div[1]/div/div/h3/a”)).click();
}
}

FindElements method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package seleniumTutorial;
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 FindelementFindElements {
public static void main (String [] args){
// Open browser
WebDriver driver = new FirefoxDriver();
// To maximize the window
driver.manage().window().maximize();
// Open application
driver.get(“https://www.google.co.in/?gws_rd=ssl#q=softwaretestingmaterial.com”);
// Get the list of all links
List link = driver.findElements(By.xpath(“//*[@id=’rso’]/div/div/div/h3/a”));
// Using for loop to display the text of all the links
for(WebElement element:link)
{
System.out.println(element.getText());
}
// Click on the first link
driver.findElement(By.xpath(“//*[@id=’rso’]/div/div/div/h3/a”)).click();
}
}