๐งญ Complete Guide to Selenium Locators & Element Finding Strategies
In Selenium, locating strategies are techniques used to identify web elements on a page so that actions (like click, type, etc.) can be performed on them. Selenium provides multiple locators to interact with HTML elements.
Below is a complete list of Selenium locators, with details, HTML examples, and code snippets for each.
๐ 1. ID
✅ Best when the element has a unique ID.
๐ง HTML:
<input id="username" type="text">
๐งช Selenium (Java):
driver.findElement(By.id("username")).sendKeys("testuser");
๐ 2. Name
✅ Useful when the element has a unique name attribute.
๐ง HTML:
<input name="password" type="password">
๐งช Selenium:
driver.findElement(By.name("password")).sendKeys("mypassword");
๐ 3. Class Name
⚠️ Use only when class is unique or combined with other locators.
๐ง HTML:
<button class="login-button">Login</button>
๐งช Selenium:
driver.findElement(By.className("login-button")).click();
๐ 4. Tag Name
✅ Used when selecting elements like all <a>
, <div>
, etc.
๐ง HTML:
<a href="/home">Home</a>
<a href="/about">About</a>
๐งช Selenium:
List<WebElement> links = driver.findElements(By.tagName("a"));
๐ 5. Link Text
✅ Exact match for anchor link text.
๐ง HTML:
<a href="/login">Login here</a>
๐งช Selenium:
driver.findElement(By.linkText("Login here")).click();
๐ 6. Partial Link Text
✅ Match part of the link text.
๐ง HTML:
<a href="/login">Login to your account</a>
๐งช Selenium:
driver.findElement(By.partialLinkText("Login")).click();
๐ 7. CSS Selector
✅ Powerful, fast, and flexible. Supports ID, class, attributes, child, sibling, etc.
๐ง HTML:
<input type="text" id="user" class="form-input" name="username">
๐งช Selenium (Examples):
// By ID
driver.findElement(By.cssSelector("#user"));
// By class
driver.findElement(By.cssSelector(".form-input"));
// By attribute
driver.findElement(By.cssSelector("input[name='username']"));
// By tag and class
driver.findElement(By.cssSelector("input.form-input"));
๐ 8. XPath
✅ Most flexible and powerful. Supports relative/absolute paths, axes, and conditions.
๐ง HTML:
<input type="text" name="email" id="emailField">
๐งช Selenium (Examples):
// Absolute XPath (not recommended for dynamic pages)
driver.findElement(By.xpath("/html/body/div/input"));
// Relative XPath (recommended)
driver.findElement(By.xpath("//input[@id='emailField']"));
// Multiple conditions
driver.findElement(By.xpath("//input[@type='text' and @name='email']"));
// Text-based
driver.findElement(By.xpath("//button[text()='Submit']"));
๐ Summary Table
Locator Strategy | Method | When to Use |
---|---|---|
ID | By.id() |
Best for unique elements |
Name | By.name() |
Good if name is unique |
Class Name | By.className() |
Use if class is unique or combined with others |
Tag Name | By.tagName() |
Use for lists or bulk element handling |
Link Text | By.linkText() |
Exact match of link text |
Partial Link Text | By.partialLinkText() |
Partial match of link text |
CSS Selector | By.cssSelector() |
Preferred for speed and flexibility |
XPath | By.xpath() |
Powerful; ideal for complex or dynamic elements |
Comments
Post a Comment