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.
- Link Text
- Partial Link Text
- Tag name
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”.