What is the difference between findElement and findElements in Selenium Webdriver?

+1 vote
May 8, 2019 in Selenium by Nilaabh
2,911 views

2 answers to this question.

0 votes

Hi Nilaabh, Selenium Webdriver uses findElement and findElements methods to find locators of any web element. Now the difference between them is that the findElement command takes in the By object as the parameter and returns an object of type WebElement

By object in turn can be used with various locator strategies such as ID, Name, Class Name, XPATH etc. This method throws exception NoSuchElementException if there are no elements matching the locator. Following is the syntax of findElement command in Selenium web driver:

WebElement elementName = driver.findElement(By.LocatorStrategy("LocatorValue"));

While, findElements command takes in By object as the parameter and returns a list of web elements. It returns an empty list if there are no elements found using the given locator strategy and locator value. Returns an empty list if there are no web elements matching the locator strategy. Below is the syntax of findElements command:

List<WebElement> elementName = driver.findElements(By.LocatorStrategy("LocatorValue"));
answered May 8, 2019 by Anvi
• 14,110 points
0 votes

FindElement Command -This method locates for the first web element on the current web page matching the criteria mentioned as parameter.  If the web element is not found, it will throw an exception - NoSuchElementException 

Syntax - findElement(By arg0):WebElement - WebDriver

Command - driver.findElement(By.xpath("Xpath location"));

FindElements Command - This method locates all the web elements on the current web page matching the criteria mentioned as parameter.   If not found any WebElement on current page as per given element locator mechanism, it will return empty list.        Syntax - findElements (By arg0):List<WebElement>

More details - https://configureselenium.blogspot.com/2019/05/findelement-vs-findelements.html

answered Feb 10 by anonymous

Related Questions In Selenium

0 votes
2 answers
0 votes
1 answer

What is the difference between WebDriver and RemoteWebdriver in Selenium?

Hey Ishan, the difference between Webdriver and ...READ MORE

answered May 10, 2019 in Selenium by Pratibha
• 3,690 points
7,165 views
0 votes
1 answer
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,520 points
10,546 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,520 points
3,829 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31 in Selenium by Sri
• 2,970 points
4,809 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
2,774 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2 in Selenium by Sri
• 2,970 points
3,151 views
0 votes
1 answer

What is the difference between getWindowHandle() and getWindowHandles() methods in Selenium WebDriver?

Hey Jennifer, difference between getWindowHandle() and getWindowHandles() ...READ MORE

answered Jun 3, 2019 in Selenium by Anvi
• 14,110 points
6,487 views
0 votes
1 answer

What are the differences between POM and PageFactory in Selenium Webdriver?

Hi Raveena, Page Object Model (POM) and ...READ MORE

answered Jul 17, 2019 in Selenium by Anvi
• 14,110 points
6,039 views