Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

Different between findElement and findElements method in selenium

 Different between findElement and  findElements method in selenium :

 Locating elements in Selenium WebDriver is done by using the findElement() and
findElements() methods provided by WebDriver and WebElement class.

The findElement() method returns a WebElement object based on a specified search
criteria or throws up an exception if it does not find any element matching the search criteria.

The findElements() method returns a list of WebElements matching the search criteria.
If no elements are found, it returns an empty list.

Note: This method is useful when we want to work with a group of similar elements. For example: we can get all the links displayed on a page or get rows from a table, and so on. 

The findElements() method returns all the elements matching with the locator specified
as a list of WebElements. In Java, we can use the List class to create an instance of list of
WebElements.
List<WebElement> links = driver.findElements(By.tagName("a"));
The size() method of the List class will tell us how many elements are there in the list.
assertEquals(4, links.size());
We can iterate using this list in the following way, getting a link and printing its target value:
for(WebElement link : links)
System.out.println(link.getAttribute("href"));

Post a Comment

0 Comments