Recommendations on Choosing the Locators in Selenium

Knowledge which helps to understand the importance of types of locators in Selenium – it’s the first and main step in the way of using such product “at maximum speeds”. And knowledge on their correct usage – it’s the main skill of working with this utility, as the rest knowledge in the process of interface with Selenium is just trivial selecting of necessary libraries and extensions and also using some backdoor ways.

It means that the rest is a special field which is more likely referred to programming skills. That’s why in the next chapters we will try to completely understand in which situation and what locator is better to use.

Types of Locators

In fact, all available today types of locators can be divided into:

  • Link – as the name implies, is used only for links;
  • Id
  • name
  • DOM
  • Xpath

It means that when we are searching for a necessary locator for an object, we study its HTML structure and find unique details, such as:

  • Text fields;
  • Name of Id attribute;
  • Title of Name attribute;
  • Elements which are located nearby or parts of data which include at least 1 mentioned identifier.

In other words, it’s a basic template and, depending on its type, automated testers can freely select necessary locators, keeping completely optimal “proportions” of accuracy and speed of automated testing. But it’s not so easy as it may seem at a first sight.

locator recommendation

A practice of using Selenium as an automated tool has numerous typical situations and cases when you should thoroughly overview numerous locators types (sometimes even operations of calculation of element value).

Examples of such cases are:

  • Located hyperlink with a static set of letters (a text) or with a small fixed text block;
  • A classical object for operating the provided form;
  • A separate element inside a table which includes a particular part of homogeneous parts.

And now let’s analyze such homogeneous parts.

A separate hyperlink with a static text or a small fixed text block

So, let’s imagine that we have a particular fixed hyperlink on a static page or there are no other hyperlinks.

<a href=»»>A simple hyperlink</a>

We need to find a necessary locator selecting from prior locators which will be discussed in the “Types of locators” chapter.

What do we have? Link locator is the first one:

  • Is it used for the links? – Yes;
  • Will it help to capture a fixed object of a hyperlink? – Yes;
  • Does a locator uniquely identify an object? – Yes, as a link is static.

It means that we can surely state that a link locator is a perfect decision in such case. We can easily operate this value the process of working with links.

A Separate link with a Fixed Text Part in it

Let’s make a task more difficult.

For example, a particular date is mentioned in a text block:

<a href=»»>A simple link from November 21</a>

Logically, in the process of creating the type of link=a simple Link from a November 21, all text blocks, which operate this link, will properly function only since November 21. But paying attention to the fact that a text “A simple link from November 21” is static, a page has a static element on it.

Type of Link locator not only permanently uses a text line of a particular link but also a regular expression, which completely overlaps with a text of the hyperlink. And in such situation, we can easily use such expression as wildcards, namely “*”. And now our locator of Sample link* type will be sensitive to the date change.

Let’s repeat 3 conditions for any links:

  • Is it used for the links? – Yes;
  • Can it capture or “limits” a text of a hyperlink – Yes;
  • Is a locator of an object uniquely checked? – Yes, as a link is static.

It means that everything stays the same.

Some Links with the Same Text Block or One Fixed Part

Let’s imagine that we have 2 types of links of such type:

<a href=»»>A simple link from November 21 </a>

And they have all same things: a text and used attributes (and even according to a design, look and function in the same way). If we use a locator link=Sample link*, only the first link will be captured but how to check the functionality of the second one? Let’s first check a compliance of our 3 mentioned criteria for the links:

  • Is it used for the links? – Yes;
  • Can it capture a fixed text of a hyperlink? – Yes;
  • Is a locator of an object uniquely checked? – No, as the same objects mean that they are not unique.

What we have: we must find something new and unique. Let’s analyze the rest types of locators which we have:

  • Name and id don’t fit;
  • Dom – is also not a good variant, especially when we talk about the binding of locators to a text block or separate element;
  • CSS is not really helpful;
  • XPath is a special locator which helps to qualitatively detail a place of an object, for example, in dom structure.

That’s it, a task is defined: to properly calculate XPath. What is good, XPath can help to check a test block of an element, index and value of a particular attribute. In our case, we should capture a link with “a Simple link” text line with the mentioned index no.2 (and no binding to a particular parent element or an object).

Usually, a structure of such locator will look like that:

locator xpath recommendation

Again, check 3 conditions of locators for the links:

  • Is it used for the links? – Yes;
  • Can it capture a fixed text of a hyperlink? – Yes;
  • Does a locator uniquely identify an object? – Yes, as a link is static.
  • Can it capture a fixed text of a hyperlink? – Yes;

XPath is the best variant for such case. Let’s discuss more complex situations which automated testers can face in the process of building automated tests for their projects.

A Classical Element of Operating a Provided Form

We often meet such elements which are very comfortable to work with during the process of testing. It can be all possible text boxes, buttons, images, animation blocks, etc. We should mention an average criterion for such group of objects: these “icons and figures” are calculated only in 1 value. Let’s discuss them more thoroughly. For example:

<input type=text id=also_id name=also _name />

Then choose a list of available locators according to available priorities and try to build the best one for such situation. Let’s return to the previous list:

  • Link is not a good fit as this object can’t be regarded as a link.
  • Id – a perfect choice! It has attribute id;
  • Name is also a good fit as an attribute has its own unique name;
  • CSS – is quite good;
  • Dom is also good as it has all necessary attributes;
  • XPath is exactly a right choice!

Then build id into a locator:


Unfortunately, id attribute is used not always and not in all cases. Sometimes we have cases when some elements have the same id in one structure of HTML document.

But if we talk about objects (elements) of operation such as typing box or dropdown list of categories, they usually have their own attribute “name”, which must be checked by a system before sending the information to a server.

And very often in such case, we can face the following situation:

<input type=text name=also_name />

Let’s discuss what locators help to describe this or that element:

  • Link does not fit as this object can’t be regarded as a hyperlink;
  • Id is not a good fit as id is not specified;
  • Name also does not fit as an attribute has its unique name;
  • CSS is rather good variant but only in case if there are no complex bindings to the hierarchy on different structure levels;
  • Dom is also a locator;
  • XPath is a good choice.

A Separate Element Inside a Table Which Has a Particular Part of Homogenous Parts

Almost all modern software components and systems, in some way, work with databases. Usually, the information mentioned there is displayed in a form of tables. And if we talk about tables, then there are special forms which require particular settings in the process of working with them.

As a result, any web application has numerous hierarchy structures which have similar elements and their number can permanently increase. In such case, it’s very hard to use a fixed locator for one particular element and it’s useless in case of working with tables.

Let’s discuss one quite good example of creating one operation for a group of objects of a particular type.

For example, HTML markup which has such fragment of a code:

<td><input type=checkbox name="chbox1"/></td><td> 1</td>
<td><input type=checkbox name="chbox2"/></td><td>2 </td>
<td><input type=checkbox name="chbox3"/></td><td>3 </td>
<td><input type=checkbox name="chbox4"/></td><td> 4</td>

Of course, we can use a locator for every object. But it can have not only 4 but even 44 options which makes a work more difficult. It’s logical to find a multifunctional locator, which can cover all data on a page without numerous variations.

To not long describe a logic of defining such locator, we can admit that its task has only 2 elements: CSS and XPath.

And we get such binding:


But it’s a typical type of a locator which is a good variant only for the first object of our list. And in order to make it operate with the second, the third and the fourth one, it must get a special id which is defined in XPath.
For example,

xpath recommendation

which turns only to the third check-box mentioned on a page. To properly get all sums, we use getXPathCount method. As a result, obtained software code performs actions of all similar elements which are on one page:

xpath recommendation

And it’s only a part of all possibilities and technologies which locators of Selenium utility can offer and they are regarded as one of the most popular and modern programs for a qualitative automated testing of a project.

Any quality control company should use such tools, which allow to perform qualitative and possibly efficient testing of software development for any business tasks.

Selenium Tutorials: 

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.