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:
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:
List link = driver.findElements(By.xpath("Value of Xpath"));
Let’s see a practical example – FindElement and FindElements methods in Selenium WebDriver:
FindElement method:
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:
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(); } }
List link = driver.findElements(By.xpath(“//*[@id=’rso’]/div/div/div/h3/a”));
should be like this otherwise it ill show type mismatch issue.
List link = driver.findElements(By.xpath(“//*[@id=’rso’]/div/div/div/h3/a”));
Whats your point Ashish. I didnt get you.
if i want to know
1. total no. of links present
2. text of any nth link
then how to code?