Home Testing Selenium Tutorial Selenium Locators

Selenium Locators


Selenium Locators

Selenium needs to access the web elements for automating the tasks related to them. For finding web elements on the webpage, it uses the concept of Locators. Locators define different ways to reach to the web element on a webpage using some of its attributes. After finding the web elements, Selenium automates the operations like click, text entry, select the checkbox, etc.

There are many types of locators available. It is imperative to select the proper locator technique which makes the processing faster and is easier to maintain. This article talks about different types of locators and how to select the right locator for different scenarios.

Selenium locators:

  1. ID
  2. Name
  3. Class
  4. Link Text
  5. Partial Link Text
  6. CSS
  7. XPath
  8. Tag name

findElement() method

Selenium provides the findElement() method which helps with locating web elements on the webpage. This method uses different locator types like ID, name, XPath, etc to find the elements. The syntax of this method is

WebElement element = driver.findElement(By.locator technique(“locator value”)); 

For instance, driver.findElement(By.id(“username”));

Here, Selenium will find the element with the use of id locator having the value of id as “username”.


Please enter your comment!
Please enter your name here