Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
Locators
3.
Locating Strategies
3.1.
By ID
3.1.1.
Example
3.2.
By Name
3.2.1.
Example
3.3.
By XPath
3.3.1.
XPath Absolute
3.3.2.
XPath Attributes
3.4.
By CSS
3.5.
By DOM
3.5.1.
getElementByID
3.5.2.
getElementByName
3.5.3.
dom:name
3.5.4.
dom:index
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Locating Strategies in Selenium

Introduction 

When working with Selenium we target certain locations in the web application and locate them as web elements. However, to isolate the particular element we want to locate we need to use certain strategies or criterias by which we can uniquely identify the particular web elements. Using these strategies depend mostly on how we want to define the unique element in the web application.

In this blog, we will learn about the different ways in which we can locate the elements in the application we are building and when we should use them particularly.

Locators

When making a webpage we sometimes need to isolate certain elements from the page individually. The pieces of code that make this easier for us and identifies these elements are known as locators. Locators are what are used as the basic building blocks of automation scripts built in Selenium. Thus, locators are one of the most powerful tools when it comes to finding target values of the action in Selenium. 

Finding the correct element to be used in the script is the first step to any good testing. If we use an incorrect locator in the script this may lead to script failure. Thus, we need to choose the correct locator strategy for certain automations in workflows. 

Locating Strategies

While working with Selenium, we usually have six modes of specifying location in a particular webpage to locate certain unique web elements.

  • By Identifier
  • By ID 
  • By Name
  • By XPath
  • By CSS
  • By DOM

By ID

When we are able to find id attributes given to certain elements and can isolate them from the same. When using this strategy, the locator is very explicit and locates the first element with the matching id attribute and is used. 

Example

First we need to open the Selenium IDE and click on the command text box in the Test Script Editor box.

Then, our first command would encompass:

Command: open

Target: https://facebook.com/login

Upon executing this command we will be redirected to the facebook login page.

Now we need to find our first id to be isolated and tested. Here, we inspect the username text box and find the id “email”.

Now our next command will be:

Command: click at

Target: id=email

We can add another test as well:

Command: type

Target: id=email

Next we simply need to run the current tests using the button on the toolbar menu of the IDE and we will be able to get the results in the Log panel at the bottom.

By Name

When using name we have to be careful of the fact that this is not an unique attribute and thus only the first element with the matching name attribute is located.

Example

The test here is done similar to that of the previous test the only difference being that the target will contain values like, 

Target: name=email.

By XPath

XPath is what is used to locate nodes in XML Documents. Thus, here it acts like a substitute to id and name attribute for the elements that we want to locate.

There are two ways to represent the paths:

  • Absolute
  • Attribute

XPath Absolute

In absolute we need to mention the whole XPath location from the root HTML tag in the target of the command. However, this method is not very much recommended because if the location is slightly changed the script won’t be able to locate the element and the script may fail.

Syntax: //html/body/tag1[index]/tag1[index]/.../tagN[index]

XPath Attributes

Unlike absolute, in attributes we only need to locate the web elements using their different attributes which are unique and can still be used even though their location is changed.

Syntax: //htmltag[@attribute1=’value1’ and @attribute2=’value2’]

By CSS

In this method, we need to describe how we have formatted the document in markup that we are looking up in the locator. Thus it includes a combination of HTML tag, id, class and attributes and thus has six other modes to identify and locate the web elements. 

  • ID
  • Class
  • Attributes 
  • Substring
  • Inner-text
  • ID/Class & Attributes

These methods are pretty self explanatory so we will not dive deeper into it.

By DOM

DOM basically means Document Object Model and specifies the structure of our HTML elements and how they can be accessed.

In DOM there are basically four methods:

  • getElementByID
  • getElementByName
  • dom:name
  • dom:index

getElementByID

Here, we simply call the document object and the getElementById method and use it.

Syntax: document.getElementByID(“id”)

getElementByName

Here, we simply call the document object and the getElementByName method and use it.

Syntax: document.getElementByName(“name”)[index]

dom:name

Here we can specify the attribute against which the value must be matched and located, and thus the element we want to access.

Syntax: document.forms[“value”]

dom:index

We can also use integers as the index value of the dom element we want to access.

Syntax: document.forms[index]

FAQs

1. What is Selenium?

Ans: Selenium is a software testing framework for web applications that can also be used to write scripts in multiple languages and in most browsers.
 

2.What are the advantages of Selenium?

Ans: Some of the benefits of Selenium are Language Support, Open Sourcing, Multi-Browser Support, etc.

 

3. Which is the fastest Selenium Locator?

Ans: The ID locator is the fastest Selenium Locator and the most preferred one as well as ID is a unique attribute in the DOM.

 

4. Why is ID locator faster than XPath?

Ans: ID locator is considered as an unique key where as XPath is created using relative path and position of the elements.

Key Takeaways

In this blog, we discussed what are the different strategies used by Locators in Selenium.

You may want to learn more about JUnit annotations in Selenium here.

Learning never stops, and to feed your quest to learn and become more skilled, head over to our practice platform Coding Ninjas Studio to practise top problems, attempt mock tests, read interview experiences, and much more.!

Happy Learning!

Live masterclass