findElement and findElements Commands with Examples

Whenever you plan to automate any web application using Webdriver, you usually start by finding the HTML elements on the page. Then, you incline to choose the right strategy to locate them. Webdriver defines two methods for identifying the elements; they are findElement and findElements.

We have given many examples of Webdriver’s findElement() command in our previous blog posts. Now, you’ll see a detailed overview of both the findElement and findElements methods.

Next, we’ll also help you explore the different locator strategies that you can use along with these methods.

One more point which you should clearly understand is the difference between the findElement and findElements methods. You can find solutions to all these points in this post.

Here is the list of topics we are covering today.

Table of Content.

  1. Find locators using Firefox’s Inspector and FirePath Tool.
  2. The difference between findElement and findElements methods.
  3. Understand multiple By strategies to access locators.

So, let’s first see an animated GIF to demonstrate the Firefox’s Inspector and FirePath Tool and then we’ll review the key differences between the findElement and findElements methods.

1- Find locators using Firefox’s Inspector and FirePath Tool.

We’ve captured a small set of actions to showcase how can you use the Firefox’s internal inspector tool to find a unique locator. Then there is the FilePath add-on demo to determine or verify the XPath of any element.

findElement and findElements Demo

Firefox Inspector and FirePath Demo.


2- The difference between findElement and findElements methods.

findElement() method.

  • You can use this command to access any single element on the web page.
  • It returns the object of the first matching element of the specified locator.
  • It throws a NoSuchElementException exception when it fails to find If the element.
  • Its syntax is as follows.
    • WebElement user = driver.findElement(“User”));


findElements() method.

  • First of all, we don’t use it frequently as its usage is very limited.
  • It gives back the whole list of all the elements matching the specified locator.
  • If the element doesn’t exist or not available on the page then, the return value will be an empty list.
  • Its syntax is as follows.
    • List<WebElement> linklist = driver.findElements(By.xpath(“//table/tr”));


Code Example – findElement and findElements methods.

Now let us provide a straight and practical example of findElement and findElements methods to give you a clear picture.


3- Understand multiple By strategies to access locators.

Webdriver references the Web elements by using the findElement(By.<locator()>) method. The findElement method uses a locator/query object known as <“By”>. There are various kinds of “By” strategies which you can utilize depending on your requirements. Let’s go through them one by one.

3.1- By ID.

Command: driver.findElement(<element ID>))

According to this strategy, the By method will return the first element matching the id attribute value. If it doesn’t find any matching element then, it’ll raise a NoSuchElementException. Using element Id is the most preferred way to locate an element, as usually Ids have unique values. But in a large project which is in the development phase, the developers may lead to use non-unique ids or auto-generating ids. Ideally, they should avoid the occurrence of any of these conditions.



3.2- By Name.

Command: driver.findElement(<element-name>))

By.Name() is another useful way to locate an element but it is also prone to same the issue as we’d seen with the Id. The issue was what if the web developer could’ve used non-unique/auto generated names on a page.

This strategy states that the <By.Name()> method will return the first element matching the name attribute. If it’s not able to locate it then, it’ll throw a NoSuchElementException.



3.3- By Class Name.

Command: driver.findElement(By.className(<element-class>))

This method gives you the element which matches the values specified in the “class” attribute. If the element has more than one class, then this method will match against each one of them.



3.4- By TagName.

Command: driver.findElement(By.tagName(<html tag name>))

You can use this method to find the elements matching the specified tag name. You may call this method when you want to extract the content within a tag or wish to perform any action on the tag element.



3.5- By LinkText or PartialLinkText.

Command: driver.findElement(By.linkText(<link text>))
                       driver.findElement(By.partialLinkText(<link text>))

Using this method, you can track the elements of “a” tags (Link) with the link or partial-link names. Apply this method, when you have the link text that appears within the anchor tag.



3.6- By cssSelector.

Command: driver.findElement(By.cssSelector(<css-selector>))

With this method, you can use a CSS selector to locate the element. The CSS is cascading style sheet; it sets the styles for a web page and its elements.



Apart from the above examples, you can also perform partial matching on Html attributes.


3.7- By XPath.

Command: driver.findElement(By.xpath(<xpath>))

You can try this method when you want to locate an element using the XPath query. XPath is a way to traverse through the of document object model, gives you the ability to select specific elements, attributes, or a section of an XML document.




We came out with this post because we wanted you to know the nitty-gritty of the findElement and findElements methods. Another concept we addressed was to choose between the different By.locator() strategies. You can master these concepts only by practicing with real-time web applications. Here is a post where you’ll see a lot of demo websites to practice Webdriver commands.

We expect you to apply this knowledge while you work and create better automation solutions.