top of page

What are Locators in Selenium?

Locators are a way or technique to identify HTML elements of a web page such as button, textbox, image etc. Locators can be unique or duplicate. In this article we will discuss what are different types of locators, how to identify locators and how we can use these locators for automation framework.

What are different types of locators?

The different types of locators are given below:

1. Id –Finds elements whose ID attribute matches the search value. This is most commonly used and fast and effective.

2. name –Finds elements whose NAME attribute matches the search value.

3. ClassName – Finds elements whose class name contains the search value. Class is used to identify multiple elements. Not recommended

4. Link Text –Finds anchor elements whose visible text matches the search value. Link text is preferred to use over partial link text.

5. partialLinkText – Finds anchor elements whose visible text contains the search value. If multiple elements are matching, only the first one will be selected.

6. tagName – Finds elements whose tag name matches the search value.

7. CSS Selector– CSS is used to create style rules for webpages and can be used to identify any web element. Finds elements matching a CSS selector.

8. xpath – xpath is the language used to query the XML document. The same can uniquely identify the web element on any page. Finds elements matching an XPath expression.

Java syntax for locating elements in Selenium WebDriver















CSS Selector




Let's look at some examples:

Locating web elements By id

Right click on the textbox and then click on Inspect on the context menu. The document object model will open. In the DOM the Id attribute for the web element is highlighted as shown in the image below. Note: If the webpage is dynamic and the id’s generated are dynamic, we should not use locator by Id. So, if the Id has randomly generated number, it is a dynamic hence, we should not use it.

Jave Syntax:




Locating web element By name

In the DOM the name attribute for the web element is highlighted as shown in the image below.

Java Syntax:




Locating web element By classname

In the DOM the classname attribute for the web element is highlighted as shown in the image below.

Java Syntax:



driver.findElement(By.className(“oxd-button oxd-button--medium oxd-button--main orangehrm-login-button”));

Locating web element By Link Text

In the DOM the Link Text attribute for the web element is highlighted as shown in the image below.

Java Syntax:




Locating web element By Partial Link Text

In the DOM the Partial Link Text attribute for the web element is highlighted as shown in the image below.

Java Syntax:




Locating web element By tagName

In the DOM the tagName attribute for the web element is highlighted as shown in the image below.

Note: Tag name can be used to locate multiple web elements

Java Syntax:




Customized Locator

The 2 customized locators are:

  • Css Selector

  • XPath

Locating web element By Css Selector

CSS selector is used when there is no unique id, name or class. CSS selectors locates the path to an element with a class, ID, or any other attribute containing the information needed.

There are different combinations for CSS selector.

  • tag id - #

  • tag class - .

  • tag attribute - []

  • tag class attribute - .[]

Note: CSS Selector locators are usually used for complex webpages where id is not unique. The combination will locate the element. if it located multiple elements only the first one is considered.

Java Syntax


Example by using tag and id:


Example by using tag and class:


Example by using tag and attribute:


Example by using tag class and attribute:


Locating web element By XPath

Right click on the textbox and click Inspect on the context menu. The DOM will open. Right click on the highlighted part and the select Copy and then click Copy XPath. The Relative XPath is highlighted on the image below.

There are 2 types of XPath:

  • Absolute XPath - Absolute XPath contains the complete path from the root element. An absolute location path starts with a slash ( / ) .

  • Relative XPath - Relative XPath starts from the middle of HTML DOM structure. It starts with double forward slash (//). Relative XPath can search elements anywhere on the webpage.

Note: Relative XPath is preferred to use over Absolute XPath as it is easy for maintenance in long run. As, new elements may be added, or some existing elements may be removed from the webpage, which can break the Absolute XPath.

Java Syntax:



Using Absolute XPath


Using Relative XPath



  1. There are 8 types of locators in Selenium, By Id, By name, By ClassName, By LinkText, BY PartialLinkText,t By tagname, By Css Selector, By Xpath.

  2. Do not use locators to locate auto-generated elements on the web page. Sometimes in a dynamic web environment, element attribute properties are generated at run time.

  3. Id and Name attributes are preferred over other type of locators.

  4. If elements are not found by the general locators like ID, name, class etc., then XPath is used to find an element on the web page.

  5. Relative XPath are preferred over Absolute XPath as it is easy for maintainability.

  6. CSS selectors are fast and effective when there are no unique elements.

  7. Mostly we use the basic locators to find the web elements, we use custom locators only when basic locator fails to find the web elements.

Hope this article gives you an idea about locators in Selenium. Happy Learning!!

223 views0 comments


Mit 0 von 5 Sternen bewertet.
Noch keine Ratings

Rating hinzufügen
bottom of page