TechBeamersTechBeamers
  • Viral Tips 🔥
  • Free CoursesTOP
  • TutorialsNEW
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing Tutorial
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
  • Top Interviews & Quizzes
    • SQL Interview Questions
    • Testing Interview Questions
    • Python Interview Questions
    • Selenium Interview Questions
    • C Sharp Interview Questions
    • Java Interview Questions
    • Web Development Questions
    • PHP Interview Questions
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
  • ToolsHOT
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
TechBeamersTechBeamers
Search
  • Viral Tips 🔥
  • Free CoursesTOP
  • TutorialsNEW
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing Tutorial
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
  • Top Interviews & Quizzes
    • SQL Interview Questions
    • Testing Interview Questions
    • Python Interview Questions
    • Selenium Interview Questions
    • C Sharp Interview Questions
    • Java Interview Questions
    • Web Development Questions
    • PHP Interview Questions
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
  • ToolsHOT
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
Follow US
© TechBeamers. All Rights Reserved.
Selenium Tutorial

How to Select the Selenium Locators

Last updated: Feb 24, 2025 11:21 am
Harsh S.
By
Harsh S.
Harsh S. Avatar
ByHarsh S.
Hello, I'm Harsh, I hold a degree in Masters of Computer Applications. I have worked in different IT companies as a development lead on many large-scale...
Follow:
No Comments
2 months ago
Share
7 Min Read
SHARE

In the previous blog post, we walked you through the eight essential Selenium locators and explained some of the best examples for understanding the locators. Today, we are going to discuss the core ingredients of a successful Selenium automation project. We’ll focus on certain ways to select the right Selenium locators for web elements.

Contents
How to Select the Selenium Locators?Concise and Compact LocatorsResilient to Web UI ChangesIndependent of the Changes in Element PropertiesFinal Word: How to Select the Selenium Locators

Now the question is why do we need to choose among the multiple locators available in Selenium? The simple answer to this query is first Selenium gives us options to choose from many locators. Secondly, we have to select the locators on a case-by-case basis. Sometimes, ID/Name doesn’t work, but the CSS does. In some cases, XPath is left as the only choice that we’ve to make. So, we can’t ignore the ability of the different types of Selenium locators. We need to use them wisely as the situation demands.

Apart from all we said above, we can’t undermine the scenario when the application UI gets changed. This change directly impacts our test automation and the usage of locators. The changes would undoubtedly lead to breaking some of the locators used in the project. Hence, it is important for us to decide upon a locator strategy that has the potential to minimize the locator seepage during changes in the application.

How to Select the Selenium Locators?

Select the Selenium locators for Web Elements

Here are some key points that you should consider when you select a good element locator. Let’s open up the topic and start to review the solution we are proposing.

Concise and Compact Locators

You must keep a Selenium locator as short as possible. It would make your code cleaner, reduce complexity, and help in the long-term maintenance of the project. Additionally, it would lower the chances that your locators could fail from the continuous changes happening in the application.

Check out the following HTML code.

Now, there are three possibilities you can explore for a good element locator in Selenium. Three possible Selenium locators are as follows:

By.id(“selenium”)

By.xpath(“//div[@id=’selenium’]”)

By.xpath(“//*[@id=’selenium’]”)

Now, let’s consider a case of an ongoing customer project where you are responsible for its automation. The requirement is to have a div identifier which gets changed with every build. And the developer adds a timestamp to the div id. You can verify this from the below HTML example. The HTML contains a fixed class, but the identifier here is dynamic and bears the timestamp of the build. So, we have to select a locator that can manage the variable part of the id.

The sample HTML code is as follows.

<div id=”selenium-04102016″ class=”tutorials”>
</div>

In the next statement, you will figure out the real power of the XPath locator. The proposed solution to access the element with variable id is available below.

The proposed solution is as follows:

By.xpath("//div[contains(@id, 'selenium')]")

Resilient to Web UI Changes

Let’s consider that you are facing a relatively complex scenario in your Selenium projects. For example, you need to find the locator of an element that doesn’t have an HTML ID. In such a case, you tend to use the ‘copy XPath’ option available in the browser developer tools. But you should only follow this option when the HTML IDs are present in the page source. If that’s not the case, the ‘copy XPath’ option will leave you with something like the below locator.

The example of a complex Selenium locator is as follows:

/Html/body/div/div/div/div[3]/div/div/div[3]/div/span

There is no second thought about the degraded quality of the above locator. Any minor change in the chain of its elements is sufficient to lead to an incorrect result.

Being a Selenium expert, you must learn to tackle such situations. The HTML code given below would help to understand more about the problem. Let’s see how to handle the chain of locators.

<div class="tutorials">
   <div class="selenium">
      <div role="locators">Test1</div>
   </div>
   <div class="tutorials">
      <div class="selenium">
         <div role="locators">Test2</div>
      </div>
   </div>
</div>

As an illustration, let’s assume you’ve to locate the div containing the “locators” role. You can’t directly lead to the div with the “locators” role because there exist two divs in between. Instead, you can do it by first locating the “tutorials” div and then descending to the div having the “locators” role.

Check out the below two answers to the above problem.

By.xpath(“//div[contains(@class, ‘tutorials’)]/div/div”)

By.xpath(“//div[contains(@class, ‘tutorials’)]//div[@role=’locators’]”)

Independent of the Changes in Element Properties

HTML elements used to have many properties or attributes. What would you do to make your locators work when an HTML element loses one of its properties? You need to devise a locator strategy that is independent of the locator attributes. Let’s see how can you do it with the help of the below example.

Let’s demonstrate the scenario using the following code.

<div class="tutorials selenium locators">
   <div class="post">
      <div role="author">List</div>
   </div>
</div>

Now, let’s assume you are searching for the class “selenium.” For this, it is sufficient to look for the class of type “selenium. You should exclude the “tutorials” and “locators” classes. It is because checking for the additional classes would yield you no benefit. Instead, your code would break when any of these two properties get changed.

So, don’t search for the below element locator. Please also check out the XPath expression that we don’t recommend.

By.xpath("//div/[@class='tutorials selenium locators']")

Instead, we suggest you use the XPath expression as given below.

By.xpath("//div/[contains(@class, 'selenium')]")

Final Word: How to Select the Selenium Locators

We wish that our readers not only have the bookish knowledge of Selenium, but they should also be able to use it live in their workplace.

Subsequently, we believe that this Selenium tutorial will help you in doing that. And you would be able to use the Selenium locators more efficiently in your projects.

Best,
TechBeamers

Related

TAGGED:Selenium Locate Elements
Share This Article
Flipboard Copy Link
Subscribe
Notify of
guest

guest

0 Comments
Newest
Oldest
Inline Feedbacks
View all comments

List of Topics

Stay Connected

FacebookLike
XFollow
YoutubeSubscribe
LinkedInFollow

Subscribe to Blog via Email

Enter your email address to subscribe to latest knowledge sharing updates.

Join 1,011 other subscribers

Continue Reading

  • How to Install and Use FireBug and FirePathApr 16
  • Selenium Components for Web Test AutomationApr 17
  • Learn Basic Selenium Webdriver CommandsApr 18
  • FindElement and FindElements CommandsApr 20
  • Handle Checkbox & Radio Button in WebdriverApr 21
  • Implement Page Object Model in SeleniumAug 17
  • Selenium Java App to Find Blog Rank in GoogleOct 12
  • Best Selenium Practice Websites in 2025Feb 8
  • Six Steps To Setup Selenium WebDriver Project in EclipseMar 1
  • Selenium IDE Download And Installation GuideMar 12
View all →

RELATED TUTORIALS

Convert Maven Project to TestNG in Eclipse

How to Convert Maven Project to TestNG in Eclipse

By Meenakshi Agarwal
10 months ago
Create a Selenium Load Testing Demo Using BrowserMob Proxy

BrowserMob Proxy for Selenium Load Testing

By Meenakshi Agarwal
3 months ago
Demo sites for automation testing practice

20 Free Demo Sites for Automation Testing

By Meenakshi Agarwal
4 weeks ago
3 Unique Ways to Generate Reports in Selenium Webdriver

How to Generate Reports in Selenium

By Harsh S.
4 days ago
© TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
wpDiscuz