Deep Dive into Selenium Locators 🔥
📌 What Are Locators in
Selenium?
Locators in Selenium are strategies used to identify and
interact with elements on a webpage (e.g., buttons, text fields, links).
📌 Types of Selenium
Locators
Locator |
Syntax (Java) |
Best Use Case |
ID |
By.id("elementID") |
Fastest, unique elements |
Name |
By.name("elementName") |
Multiple elements may have same name |
Class Name |
By.className("className") |
Selecting elements with a common class |
Tag Name |
By.tagName("tagName") |
Finding multiple elements (like all <input>) |
Link Text |
By.linkText("Link Text") |
Clicking links with exact text |
Partial Link Text |
By.partialLinkText("Partial Text") |
Clicking links with part of the text |
CSS Selector |
By.cssSelector("cssExpression") |
More flexible than XPath, faster |
XPath |
By.xpath("XPathExpression") |
Powerful but slower than CSS |
📌 1️.Locating
Elements by ID
✅ Best for unique elements,
fastest locator
java
WebElement
searchBox = driver.findElement(By.id("search-input"));
searchBox.sendKeys("Selenium
WebDriver");
🔹 Use when: The
element has a unique id.
🔹
Avoid if: id changes dynamically.
📌 2️.Locating
Elements by Name
✅ Good alternative if id is
not available
java
WebElement
usernameField = driver.findElement(By.name("username"));
usernameField.sendKeys("myUsername");
🔹 Caution:
Multiple elements may have the same name!
📌 3️.
Locating Elements by Class Name
✅ Finds elements by class
attribute
java
WebElement
loginButton = driver.findElement(By.className("login-btn"));
loginButton.click();
🔹 Avoid if: The
class is not unique or changes dynamically.
📌 4️.
Locating Elements by Tag Name
✅ Useful for getting multiple
elements
java
List<WebElement>
allLinks = driver.findElements(By.tagName("a"));
System.out.println("Total
links on page: " + allLinks.size());
🔹 Best for:
Fetching multiple elements (e.g., all <a> or <input> tags).
📌 5️.Locating
Elements by Link Text
✅ Finds links using exact text
java
WebElement
aboutLink = driver.findElement(By.linkText("About Us"));
aboutLink.click();
🔹 Use when: The
link text is unique.
🔹
Avoid if: Link text is dynamic or too long.
📌 6️.Locating
Elements by Partial Link Text
✅ Finds links using part of
the text
java
WebElement
contactLink = driver.findElement(By.partialLinkText("Contact"));
contactLink.click();
🔹 Use when: The
full link text is too long.
🔹
Avoid if: The partial text is not unique.
📌 7️.Locating
Elements by CSS Selector (Faster than XPath!)
✅ Most preferred locator due
to speed & flexibility
java
WebElement
loginButton = driver.findElement(By.cssSelector("button.login"));
loginButton.click();
➡️ CSS Selector Syntax Examples
CSS Selector |
Meaning |
#username |
Element with id="username" |
.login-btn |
Element with class="login-btn" |
input[type='text'] |
<input> elements with type text |
div.container > ul.menu |
Direct child selector |
input[name='q'][type='text'] |
AND condition |
📌 8️.
Locating Elements by XPath (Most Powerful)
✅ Can locate elements that CSS
can't
java
WebElement
loginButton = driver.findElement(By.xpath("//button[@class='login']"));
loginButton.click();
➡️ XPath Syntax Examples
XPath Expression |
Meaning |
//tagname[@attribute='value'] |
Find element by attribute |
//*[@id='username'] |
Find element by id (any tag) |
//button[text()='Login'] |
Find button with exact text |
//input[contains(@name,'search')] |
name contains "search" |
//div[@class='menu']/ul/li[1] |
First <li> inside <ul> in <div
class="menu"> |
📌 9️.
Finding Multiple Elements
Sometimes, we need to get a list of elements, not
just one.
✅
Use findElements() instead of findElement()
java
List<WebElement>
allButtons = driver.findElements(By.tagName("button"));
System.out.println("Total
buttons: " + allButtons.size());
🔹 Use this when:
There are multiple matching elements.
📌 Best Practices for
Using Locators
✔ Use id whenever possible
(Fastest & unique).
✔ Prefer CSS Selector over XPath (Faster).
✔ Use findElements() for multiple elements.
✔ Use Explicit Waits for dynamic elements.
✔ Avoid hardcoded XPath/CSS (They may change).
🚀 Advanced Example: Using
Multiple Locators
java
WebElement
username = driver.findElement(By.id("username")); // Fastest method
WebElement
password = driver.findElement(By.name("password"));
WebElement
loginBtn = driver.findElement(By.cssSelector("button.login"));
username.sendKeys("testUser");
password.sendKeys("securePassword");
loginBtn.click();
🔥 Which Locator Should
You Use?
Scenario |
Best Locator |
Unique element |
id |
Input fields with no id |
name |
Common elements with same style |
className |
Fetch multiple elements (e.g., all <a>) |
tagName |
Links with specific text |
linkText / partialLinkText |
Faster & flexible alternative to XPath |
cssSelector |
Powerful but slower |
XPath |
🚀 Next Steps
- Try
writing locators on real websites.
- Use
Developer Tools (F12) in Chrome to inspect elements.
- Practice
XPath & CSS Selectors for dynamic elements.
Would you like examples with Explicit Waits or handling
dynamic elements? Let me know! 🚀😊