logo logo

Optimized Selenium Selector Strategies from A-Z

Optimized Selenium Selector Strategies from A-Z : SelectorsHub

Writing web automation without having good knowledge of selector strategies is like going ๐Ÿšถ to a place ๐ŸŒ whose address we don’t know.

Selectors are addresses of web elements. Thus if we want to perform an action on a web element, we first have to locate ๐Ÿ” the address of that element, and only then perform an action on it. Just as we can get an address of a place in multiple methods, in the same manner we can also locate a web element with different types of selector strategies.

There are 8 kinds of selectors (as shown in the image below) ๐Ÿ‘‡

  1. id
  2. name
  3. className
  4. linkText
  5. partialLinkText
  6. tagName
  7. cssSelector
  8. xpath

Selector Strategies


As a test automation developer, you probably know that choosing the right Selenium selector strategies can be a painful and repetitive task. You will need to work with Chrome Developer Tools, inspecting and exploring the right element strategy, copying it to your code and then boom ๐Ÿ’ฃย  The next time you will run the test it will break! ๐Ÿ’” And what about the XPath syntax? That can be a pretty painful craft in itself too, especially for folks just starting out their automation testing journey – trying to write the syntax needed to be tested, only to have characters in the wrong place or even at a blank space… ๐Ÿ˜ฅ

On average, most of us waste a very good amount of time (~1 min) ๐Ÿ•‘, just to write one XPath or cssSelector:

Time Spent on writing Selectors




















Imagine an automation tester writing more than 100 selectors per day, how much time would they be wasting just in writing selectors ๐Ÿ—‘๏ธโœ๏ธ? We are wasting this much time because:

  1. We copy-paste attribute values from the DOM while writing selectors like XPath.
  2. We have to figure out what is wrong and missing in our selectors while building them manually.
  3. We need to manually look for what kind of element it is like SVG or inside Shadow DOM, iFrame, Frame, etc.

This entire process is like writing code on Notepad, while we have so many smart editors like IntelliJ…

But not anymore โšก๏ธโœŒ๏ธ Nowadays, we have great options to achieve exactly that, but effortlessly.

TestProject revolutionizes the automation world with AI-powered self-healing test automation, which entirely focuses on Selenium selectors. It’s a first of a kind and the only FREE tool available today for Selenium powered by AI capabilities.

Elements tend to change frequently, leading to failures and flakiness of our automated test cases. But with TestProject’s smart algorithms, each element is identified not by a single selector but rather with multiple optimizedย selectors that are prioritized for you.

You don’t need to spend time looking for the best element selector strategies! And even more than that, you don’t need to be an expert with XPath or cssSelectors at all. You can simply perform the action you want, and TestProject’s Smart Test Recorder will automatically build for you 3 to 10 different element selectors for each element you are interacting with. TestProject handles even the toughest use cases, such as iFrames and purely dynamic elements ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ

Once the element is located by multiple Selenium locators, TestProject’s AI will try to fix the broken selector in runtime and in some cases autonomously predict how the element selector should look after a specific action is preformed.

You can view the selectors that were chosen for each element and their priority by clicking on the “magnifying glass”. You can also change the default priority of each selector by moving it up or down as per your needs.

TestProject AI-powered self-healing test automation

Feeling like an XPath Master and want to add your own customized selectors? Simply click on the Add locator + icon and add your custom selector (one or more) in addition to the existing ones, to make your automation even more robust ๐Ÿ’ช

There are many tools that will help you to build custom and optimized selector strategies, and I would like to show you how SelectorsHub does it very easily.

With the features like auto-suggest attributes while typing, error info for invalid selectors, shadow DOM, SVG, iFrame, etc… SelectorsHub can save you a huge amount of time and help you to learn a lot more about XPath as well as DOM. And it is an absolutely FREE, community product. Simply go ahead and download it from here: Download link.

Once these selectors are built using SelectorsHub, you can simply copy-paste them to TestProject in the Add locator text box, and you’ll be all set with the best Selector Strategy possible ๐Ÿš€

โœจ๐Ÿ‘‰ Save a seat & join me live on YouTube on Sunday 15 November to learn all about Selector Strategies @ 8:30 AM EST / 2:30 PM CET / 7:00PM IST!




About the author

Sanjay Kumar

Sanjay Kumar is on a mission to make tester’s and developer’s life easy. He has invented and created the SelectorsHub and ChroPath single-handedly, which is being used by more than half a million testers and developers in more than 180 countries and almost in every software company in the world. His creation is not just being used, it also appears in the profiles of engineers from companies like Google, Apple, Microsoft and Amazon, etc.

He is carrying a total of 9+ years of experience with companies like IBM, Goibibo, UrbanLadder, InMobi and AutonomIQ.

Currently, he is not working for any company, he is working only for the community and working full-time on SelectorsHub.


76 1 comment
  • Ram Shan November 14, 2020, 1:34 pm

    Very well written Sanjay. I really loved the way you put the analogy to explain what is selector and loved the image to explain all locator. One of the best article on selectors I would say. Really appreciate. Thank you.

Leave a Reply