Difference between findElement and findElements of Webdriver

Here you will learn the difference between findElement() and findElements() function of Webdriver (Selenium).

findElement:  
1. findElement is use to locate single element and it return WebElement  object of first occurrences element on web page.
2. If element not found it will throw exception NoSuchElementException
3. Syntax: findElement(By by).

Example:
WebElement element = driver.findElement(By.id("Home"));
element.click();

findElements: 
1. findElements is use to find multiple element on webpage,  exp: as we need to count total number of row in table
2. It return List of WebElement object of all occurrences of element.
3. If element not found it will return empty List of WebElement object.
4. Syntax: List element = findElenets(By by)

Example:
List {WebElement} element = driver.findElement(By.xpath("//table/tr"));
int size = element.size();


3 comments:

  1. Mistake in example use findElements instead of findElement

    Correct Example:
    List allElement = driver.findElements(By.xpath("//table/tr"));
    int size = allElement .size();

    ReplyDelete
  2. Nice....for looking at the mistake..

    ReplyDelete

Leave your comments, queries, suggestion I will try to provide solution