How Do You Use an Element Locator in Selenium?

Ross Jackman | December 19, 2022

How Do You Use an Element Locator in Selenium?

In Selenium Webdriver, QA engineers use locators to locate an element on the web page. This is mainly done by using findElement() and findElements() methods.

Below are the different types of locators to find an element:

  1. By XPath: Element is located by using Xpath
  2. By CSS: Element is located by using CSS selector
  3. By ID: Element is located by using the ID attribute
  4. By Name: Element is located by using the Name attribute
  5. By Class Name: Element is located by using the Class attribute
  6. By Tag Name: Element is located by using an HTML tag
  7. By Link Text: Element is located by using link text
  8. By Partial Link Text: Element is located by using the link's partial text

Below are a few locators to find elements faster and unique:

  • By ID: Element's ID is a unique attribute that can be used in creating a locator. When using an XPath if the position of that element changes later on, then we will encounter an error as “element not found” and again we have to write a new XPath to find that element. This is a very complicated and confusing task to do it again and again when locators are changed frequently. Hence to overcome this problem, we must use the “ID” locator, as even if the position of the element changes in the later stages then in the HTML page we do not have to change the locator because of its unique nature and accuracy. Thus, by using the “ID” locator, even if the element position changes the “id” of the element will remain the same or it will not change.
    Syntax: driver.findElement("value"));
  • By Name: Name attribute can also be used in locator creation. One contradiction that arises with the name locator is that multiple elements can have the same name as their attribute. For example, if we locate a check box with two-wheeler on a web page and there are multiple two-wheelers that are listed on the web page, so, therefore, the name value for scooter and motorcycle will be the same for both as “Two-wheeler”.
    Syntax: driver.findElement("value"));


This publication is for informational purposes only, and nothing contained in it should be considered legal advice. We expressly disclaim any warranty or responsibility for damages arising out of this information and encourage you to consult with legal counsel regarding your specific needs. We do not undertake any duty to update previously posted materials.

Post a Comment