Locate Web elements – Selenium WebDriver Testing Free Training

Updated on Mon 22 Aug, 2022 | 4 mins read | Free training, qa training, selenium

Welcome to the Selenium WebDriver Testing Free Training series. In the last tutorial, we talked about the following things:

  1. Get webpage title
  2. Assert the webpage title
  3. Scenarios where we can call for assert title technique.
To check the previous tutorial, please refer to the following link:

All right!! Let’s move ahead and talk about how to locate the web elements.

What is a Web element?

Before moving further, we should take a brief look at the web elements. A Web element represents an HTML element. That renders the data into an HTML page. A web page consists of different elements like buttons, input boxes, dropdown lists, links, password box, etc.

We recommend you get basic knowledge of HTML, and CSS to understand web elements and locators. You can refer following courses links:


2. CSS

Note: these courses are free.

To perform the actions on a web page (click on the button, provide the name to name text box, clear data of the text box) the first step is to find the element. To find the element, Selenium WebDriver provides the following methods:

  1. findElement() method for single web element.
  2. findElements() for more than one web elements.

These methods require the reference of the respective web element as a parameter and return org.openqa.selenium.WebElement interface object.

Now the question is

How can we get the reference of the respective element?

To get the reference of the web element, Selenium WebDriver has in-built org.openqa.selenium.By class. This class provides the mechanism to locate elements within a web page. Let’s talk about different static methods in detail:

1) By.id()

This method locates the element by the ID attribute of the web element.


driver.findElement(By.id("identifierId")).sendKeys("Located by ID");

2) By.name()

This method locates the element by the name attribute of the web element.


driver.findElement(By.name("identifier")).sendKeys("Located by NAME");

3) By.tagName()

This method locates the element by tag name.


List<WebElement> allLinks = driver.findElements(By.tagName("a"));

4) By.className()

This method locates the element by class attribute of the web element name.


driver.findElement(By.className("whsOnd")).sendKeys("Located by className");

5) By.CSSSelector()

This method uses a CSS expression to locate the element.


driver.findElement(By.cssSelector("#identifierId")).sendKeys("Located by cssSelector ");

6) By.xpath()

In our test automation codes, we generally prefer to use ID, name, className,  etc. locators.

However, sometimes we could not locate any of them on the web page, and also sometimes locators of some elements change dynamically on the web page.

In these scenarios, we need to use an XPath locator. This locator is capable to locate complex and dynamically changing web elements.

We can find the location of any element on a web page using the XML path expressions.

XPath – Key Points
  • XPath is defined as an XML path.
  • XML stands for the extensible markup language.
  • It is used to locate web elements, especially when it’s not possible to find an element  by name or ID, etc.
  • Syntax: //tagname[@Attribute = ‘value’]
driver.findElement(By.xpath("//input[@id='identifierId']")).sendKeys("Located by XPATH ");

Also read How to handle iframes using Selenium Webdriver?

7) By.linkText()

When we need to locate the link web element by text match, then here comes the linkText method to rescue.

8) By.partialLinkText() 

When we have to locate the link by partial text, then here comes the partialLinkText to rescue.



Instructor-led Training

Important note: we like to tell you that free tutorials are useful to get started but if you are interested in the best online LIVE Master of Automation Testing training program from the experts, please refer to the following link:

For Instructor-led training

Master of Functional Automation Testing

For Self-Driven training

Automation with Selenium WebDriver (Java)


So, in this tutorial, we talked about web elements, methods to find the web element and By class and its techniques to locate web elements. We also looked at syntaxes to locate the web elements with different techniques.

In the next tutorial, we will talk about methods to perform different events or actions. We will also use the above locators to find the elements to perform the actions.

Happy learning, until then.