TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: How to Select the Selenium Locators
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Selenium Tutorial

How to Select the Selenium Locators

Last updated: Feb 24, 2024 10:46 am
By Harsh S.
Share
7 Min Read
Select the Selenium locators for Web Elements
Select the Selenium locators for Web Elements
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
1- Concise and Compact Locators.2- Resilient to Web UI Changes.3- Independent of the Changes in Element Properties.

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.

Here are some key points that you should consider when you select a good element locator.

1. Concise and Compact Locators.
2. Resilient to Web UI Changes.
3. Independent of the Changes in Element Properties.

Let’s now open up the topic and start to review the solution we are proposing.

How to Select the Selenium Locators?

Select the Selenium locators for Web Elements
Select the Selenium locators for Web Elements

1- 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')]")

2- 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’]”)

3- 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

You Might Also Like

20 Demo Sites for Automation Testing

Page Object Model (POM) and Page Factory Guide in Selenium Java

Selenium 4 Relative Locators Guide

Selenium Version 4 Features – What’s New?

How to Inspect Element in Safari, Android, and iPhone

TAGGED:Selenium Locate Elements

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
Loading
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Harsh S. Avatar
By Harsh S.
Follow:
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 projects. My skills include coding in multiple programming languages, application development, unit testing, automation, supporting CI/CD, and doing DevOps. I value Knowledge sharing and want to help others with my tutorials, quizzes, and exercises. I love to read about emerging technologies like AI and Data Science.
Previous Article Use Locators in Selenium Projects 8 Ways to Use Locators for Selenium Testing
Next Article How to use FireBug and FirePath in FireFox Use FireBug and FirePath to Find Locators

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x