Differences between findElement() and findElements()

02 January, 2014

findElement()

1) It returns a single WebElement

2) If the number of elements matching with the locator is zero then it throws exeception

3) If the number of matching element is one, it returns that matching element

4) If the number of matching element is greater than zero, it returns the first matching WebElement

Ex:
driver.get("http://www.yatra.com/");
driver.findElement(By.id("datePickerDepart_dom1")).click();


findElements()

1) It returns a List of WebElements(List)

2) If number of matching element is zero it does not throw exception, it returns a List with size zero

3) If number of matching element is one, it returns a List of WebElement with size:1 and index <0>

4) If number of matching element is 10, it returns a List with size:10 and index <0-9>

Ex:
driver.get("http://www.yatra.com");
List allElements = driver.findElements(By.tagName("a"));
for (int i = 0; i < allElements.size(); i++) {
	allElements.get(i).click();
}

No comments:

Post a Comment