Why Locators are Important?
When we automate a web application, Selenium needs a way to find elements (like textboxes, buttons, links) on a web page.
Locators help us identify these elements so that we can perform actions such as click, type, select, or verify text.
---
Types of Locators in Selenium
1. ID
Every element can have a unique ID.
Example:
driver.findElement(By.id("username")).sendKeys("Lata");
2. Name
Uses the name attribute of the element.
Example:
driver.findElement(By.name("password")).sendKeys("mypassword");
3. Class Name
Finds element using the CSS class value.
Example:
driver.findElement(By.className("login-btn")).click();
4. Link Text
Finds links by their exact text.
Example:
driver.findElement(By.linkText("Forgot Password?")).click();
5. Partial Link Text
Finds links by partial matching of text.
Example:
driver.findElement(By.partialLinkText("Forgot")).click();
6. XPath
A powerful way to locate elements using XML path.
Example:
driver.findElement(By.xpath("//input[@id='username']")).sendKeys("test");
7. CSS Selector
Fast and widely used.
Example:
driver.findElement(By.cssSelector("input#username")).sendKeys("admin");