Locators in Selenium - Different Locators in Selenium

Selenium became the most widely used test automation tool for testing web based applications. First of all it is a free tool and there is no need to pay either for the license or maintenance. Thus, the testing cost are reduced considerably. This is one of the attractive features that increased the popularity of Selenium.

Second major feature that helped selenium to become very popular is that it is highly user friendly. Whenever you are executing a test, you need to find an element to perform the function. Locators are used in Selenium to find elements on the web pages. Selenium has many locators and hence this process can be performed easily.

Locators in Selenium

In order to create an automated script a GUI element should be identified well in advance and it is a very difficult task to identify the right GUI element. Different locators in Selenium makes this task easier and accurate. There are six types of locators in selenium

  • ID
  • Name
  • Link Text
  • CSS Selector
  • Document Object Model or DOM
  • XPath

You should select the type of locator depending on the nature of the application under test

Locating an Element by ID

Usually the IDs for each element will be unique and hence this method is most commonly used to locate an element.

Locating an Element by Name

This is very much similar to the above one but here instead of ID, name of the element will be used.

Locating an element by Link Text

This locator can be applied to hyperlinks only. The link can be accessed by prefixing the target with “link=” and then followed by the hyperlink.

Locating an element by CSS Selector

CSS selectors are used for locating an element based on a combination of the HTML tag, ID, class and attributes. This is a complicated process but it is the most common locator used by professionals who use advanced selenium. As HTML labels will not have IDs, names etc, in locating elements by CSS selector is done through their inner texts.

Locating an element by DOM

Document Object Model or DOM in short is the model in which HTML elements are structured. Selenium can locate the elements by DOM.

Locating an element by XPath

XPath is used as a locator to locate XML nodes. As HTML is considered an extension of XML, XPath can be used to locate HTML elements too.

Locators play an important role in selenium testing and hence you need to learn the usage of different locators to master selenium technology and become a professional selenium user.

Leave a Reply

Your email address will not be published. Required fields are marked *