๐Ÿงญ 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

Popular posts from this blog

10 automation test cases for https://www.saucedemo.com/ (Sauce Demo)

Java Roadmap for SDET Superstars!

Mastering XPath in Selenium 4 ๐Ÿš€ – Supported Functions & Axes Explained