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:
- By XPath: Element is located by using Xpath
- By CSS: Element is located by using CSS selector
- By ID: Element is located by using the ID attribute
- By Name: Element is located by using the Name attribute
- By Class Name: Element is located by using the Class attribute
- By Tag Name: Element is located by using an HTML tag
- By Link Text: Element is located by using link text
- 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.
- 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”.