What is the difference between findElement and findElements in Selenium Webdriver

+1 vote
May 8, 2019 in Selenium by Nilaabh
8,003 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,150 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 11, 2020 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
10,315 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,500 points
16,921 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,500 points
12,577 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

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

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,559 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,910 points
6,606 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 3, 2020 in Selenium by Sri
• 3,190 points
7,506 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 4, 2019 in Selenium by Anvi
• 14,150 points
13,500 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,150 points
28,452 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP