Styrow.dev
Question 21 of 64

What is the difference between findElement() and findElements()

Question

What is the difference between findElement() and findElements()

Answer

findElement(): findElement() is used to find the first element in the current web page matching to the specified locator value. Take a note that only first matching element would be fetched.

Syntax:

WebElement element =driver.findElements(By.xpath(“//div[@id=‘example’]//ul//li”));

findElements(): findElements() is used to find all the elements in the current web page matching to the specified locator value. Take a note that all the matching elements would be fetched and stored in the list of WebElements.

Syntax:

List <WebElement> elementList = driver.findElements(By.xpath(“//div[@id=‘example’]//ul//li”));