Difference between findElement and findElements Method
Posted By : Anuj Gupta | 19-Nov-2018
Selenium WebDriver is used to automate a web application and for that, we need to locate the elements on the web page. We can use different locators as per our requirement to find the element. So, let's discuss
1) findElement() method
- To access
single web element on a webpage, we use findElement() method and it returns the first matching element. - It throws NoSuchElementException exception when it fails to find the element.
Syntax:
driver.findElement(By.xpath("Value of Xpath"));
2) findElements() method
- In order to handle the bulk of web elements, we use findElements() method.
- It is used to find bulk of web elements present in webpage and its return type is list of web element i.e. List<Web Element>.
- This method is present in search context interface.
- It throws an empty list when it fails to find the web elements.
Syntax:
List wb = driver.findElements(By.xpath("Value of Xpath"));
Let's see an example on both methods.
Program for findElement() method
package Selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Test {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "/home/anuj/chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://datatables.net/examples/basic_init/filter_only.html");
WebElement new = driver.findElement(By.name(“q”)).click();
}
}
Program for findElements() method
package Selenium; import java.awt.Font; import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.server.handler.GetTitle; import com.gargoylesoftware.htmlunit.javascript.host.URL; public class Test { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "/home/anuj/chromedriver"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://datatables.net/examples/basic_init/filter_only.html"); //Get Row Count ListrowCount = driver.findElements(By.tagName("tr")); rowCount.size(); //Get Column Count List colCount = driver.findElements(By.xpath("//thead//th")); colCount.size(); System.out.println("Row count :" + rowCount); System.out.println("Col count :" + colCount); //To print table Data for(WebElement tdata:driver.findElements(By.tagName("tr"))) { System.out.println(tdata.getText()); } driver.quit(); } }
In the above example, findElement() method simply locate targeted web element and print the result in the console while findElements() method will locate the bulk of web elements present in the webpage at once and it will simply targeted those elements which are present in the xpath locator.
Conclusion:
findElement() and findElements() methods help in locating a web element or a group of elements which have the same properties. It also helps in interaction which utimately creates a bridge between a web page, test data and executes test automation script. Nowadays, we are using findElement() method commonly in our web driver software test case.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Anuj Gupta
Anuj is a passionate QA engineer and an optimistic person. He is fond of playing Chess and Basketball. He always comes up with the new ideas and is a good team player.