top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Locators in Selenium

What are locators in testing?

A locator is a query that results in 1-N elements being returned to you, the majority of the time it'll be a single element. An element is an HTML element from the page such as an input field or an anchor.

Selenium webdriver uses locators to find the web elements on web page. The following are the list of locators used by selenium.

There are 8 Locators in Selenium


1. Locating an Web Element By ID

2. Locating an Web Element By Class Name

3. Locating an Web Element By Name

4. Locating an Web Element By LinkText

5.Locating an Web Element By Partial LinkText

6. Locating an Web Element By TagName

7.Locating an Web Element By CSS Selector

8. Locating an Web Element by XPATH


It is like imagine a webelement as a student in a class.How will you identify a student ?

First by his/her ID, here also we try to identify the Webelement by its ID

Second we try to Identify the student by his/her Name,same way we identify the Webelement by its Name

Third is we will try to identify the Webelement by its Classname like which class he/she belongs to,in Webelements also we try to identify by its classname

Fourth , If we can't identify by Name,Id or classname then we can try to locate the student by its position

He/She may be in third row second position kind of likewise the Webelement can be identified by position which is the XPath

Fifth is sometimes we identify the student by the color of their outfit or how tall they will be or how her hair will be sameway we will identify the Webelement by CSS Selector

sixth is by the group of the student like is the student a dayscolar or a evening college...

here we will identify the Webelement by Tagname

Seventh is with the text of the link we will identify the Webelement

and finally by Part of the text also we can identify the Webelement which is called Partial LinkText.


Now for each locators we will see the syntax

  1. locating the webelement by ID




2.locating the webelement by Classname



3.locating the webelement by Name



4.locating the webelement by xpath:



5.locating the webelement by LinkText:



6.locating the webelement by PartialLinkText:


7.locating the webelement by Tagname:

TagName can be used with Group elements like, Select and check-boxes / dropdowns.

The syntax for this is

Select select = new Select(driver.findElement(By.tagName(“select”)));

select.selectByVisibleText(“Applets”);


8.Finally for lacating the Webelement by CSS selector is :

Below syntax will find "input" tag node which contains "id=searchInput" attribute.

css=input[id=searchInput]


Locator is a command that tells Selenium IDE which GUI elements ( say Text Box, Buttons, Check Boxes etc) its needs to operate on. Identification of correct GUI elements is a prerequisite to creating an automation script. But accurate identification of GUI elements is more difficult than it sounds. Sometimes, you end up working with incorrect GUI elements or no elements at all! Hence, Selenium provides a number of Locators to precisely locate a GUI element

Now lets see which locators will be used in which specific webelements

1. Locating an Web Element By ID: The most efficient way to locate an web element on a web page is By ID. IDs are the fastest locator and should always be the first choice even when there are multiple choices.


2. Locating an Web Element By Class Name: When there is no ID, locate the element by className.

3. Locating an Web Element By Name: If there is no Id to use in HTML code, then next preferred way to locate the web element is name attribute.

4. Locating an Web Element By LinkText: If there is Links present on Web page it is very easy to locate them but make sure, there is only one unique link on the web page. If the specified locator is duplicate then it returns first matching web element.

5.Locating an Web Element By Partial LinkText: PartialLinkText also works in the same way as Link Text. To handle dynamic links (links which keep on changing) we can use Partial Link Text. User can provide partial link text to locate an web element.

6. Locating an Web Element By TagName: TagName can be used with Group elements like, Select and check-boxes / dropdowns.

7. Locating an Web Element By CSS Selector: CSS Locator is another alternative of ID or Name locator or any other element locators in selenium webdriver software automation testing tool. Full form of CSS is "Cascading Style Sheets" and it define that how to display HTML elements on webpage of software web application

CSS is more readable than XPath. It also improves the performance. It is very compatible across browsers.

8. Locating an Web Element by XPATH : It is type of locator and it is an expression. XPATH is the path of the element in HTML tree.


There are two different types of XPath:

  • Absolute XPath.

  • Relative XPath.

Locating by DOM (Document Object Model)

The Document object model, in simple terms, is the way by which HTML elements are structured. Selenium IDE is able to use the DOM in accessing page elements. If we use this method, our Target box will always start with “dom=document…”; however, the “dom=” prefix is normally removed because Selenium IDE is able to automatically interpret anything that starts with the keyword “document” to be a path within the DOM in Selenium anyway.

There are four basic ways to locate an element through DOM in Selenium:

  • getElementById

  • getElementsByName

  • dom:name (applies only to elements within a named form)

  • dom:index


Hope now you get a clear idea of Locators in Selenium

44 views0 comments
bottom of page