How to identify locators

There are 3 basic steps to automate an application using any automation tool

  1. Launching the application
  2. Identifying the locators/elements
  3. Performing actions on locators

So far, we have discussed about launching an application. Today we will see how we can locate the elements of web applications. Selenium supports 8 types of element locators:

  • id:  This is used to locate elements with unique ID as their attribute. This is one of the more preferred ways to locate elements as ids are supposed to be unique on each page. Note that sometimes developers auto-generate ids. In this scenarios, we should avoid using id as locator. Sometimes you may not find any id for an element to work with. Hence, we have other ways of locating elements.

Format:  By.id(String id);

id

id1

  • name: This is also one of the commonly used way of locating elements. But unlike Ids, names does not have to be unique on the particular web page. So, it is less reliable method for locating elements.

Format:  By.name(String name);

name

name1

  • className: It is rarely used in real world as you will find many classes with same className on a web page. Hence, it does not guarantee uniqueness.

Format:  By.className(String className);

class

class1.PNG

  • linkText: It is used to find links on a web page. We can directly pass complete link and it will return the matching locator.

Format:  By.linkText(String linkText);

link.PNG

link1

  • partialLinkText: It is similar to linkText but in partialLinkText, we can provide a part of a link and it will return the matching locator. No need to provide complete link.

Format:  By.partialLinkText(String partialLinkText);

link

partial.PNG

  • tagName: Like className, tag names are also not unique. So, they are used mostly in scenarios where we need to get a list of elements under a specific tag name.

Format:  By.tagName(String tagName);

  • xpath:  This is the advanced way of locating elements and you will find many advanced selenium users using xpath to locate elements. It is the most common way of locating element. But it is also the most complex one. We can locate even those elements also which doesn’t have id, name or even class.

Format:  By.xpath(String xpath);

xpath

xpath2.PNG

As you can see, xpath is written in a combination of tag name and attribute. But, the most important thing about xpath is that we can use id, name attributes in xpath. So, if we know how to write xpaths, we don’t need any other way to find element. Here is one more example demonstrating the xpath by using id.

xpath1.PNG

  • cssSelector: Like Xpath, this is one of the important and powerful ways of locating elements provided in selenium. We can identify any locator based on the combinations of tag name, class, ids, names or even other attributes like value or text.

Format:  By.cssSelector(String cssSelector);

xpath

css.PNG

This concludes the basics of locating web elements in selenium. We will delve deep into xpath and locating elements in the upcoming topics. Till then if you have queries, feel free to reach out to me.

Have a great Day!!!

Leave a comment