Element Locator Strategies for Selenium WebDriver

UI automation testing relies heavi on accurately identifying and interacting with web page components, which is a common hurdle. Selenium WebDriver includes several core locator methods, plus a unified approach via the By class:

ID Locator

HTML id attributes are guaranteed unique per page, making this one of the most reliable methods.

# Clear pre-filled username, input credentials
username_field = driver.find_element_by_id("user_login")
username_field.clear()
username_field.send_keys("administrator")
# Move focus and fill password
password_field = driver.find_element_by_id("user_pwd")
password_field.send_keys(Keys.TAB)
password_field.send_keys("secure12345")

Name Locator

name attributes don’t require uniqueness, so use find_elements_by_name() to locate groups (e.g., radio buttons) and index the list.

# Select "High School" from period radio group
driver.find_elements_by_name("school_level")[2].click()
# Select "Math" from subject checkboxes
driver.find_elements_by_name("subject_select")[1].click()

Class Name Locator

Use class attributes with find_element_by_class_name(). For compound classes, pick one unique segment, or use find_elements_by_class_name() and validate context.

# Click "Grade 10" button
driver.find_elements_by_class_name("btn-secondary")[3].click()

Tag Name Locator

Locate elements by their HTML tag (e.g., <a>, <input>). This is rarely precise due to duplicate tags on most pages.

# Count all input fields
input_count = len(driver.find_elements_by_tag_name("input"))

Link Text Locator

Precisely target hyperlinks using their full visible text.

# Click "Sign Out" in the header menu
driver.find_element_by_link_text("Sign Out").click()

Partial Link Text Locator

A flexible alternative to link text, using a unique substring of the hyperlink’s visible content.

XPath Locator

A powerful, versatile strategy with multiple sub-approaches, often generated quickly via browser dev tools.

  1. Absolute Path: Traverses the entire DOM from the root (html), but breaks easily with layout changes.
  2. Attribute-Based: Uses any unique element attribute; * matches any tag.
  3. Hierarchy + Attribute: Combines parent/child relationships with attributes for precision.
  4. Logical Operators: Uses and/or to combine multiple attribute checks.
# Click login button using hierarchy and index
driver.find_element_by_xpath(".//div[@id='auth_panel']/button[2]").click()
# Click "Create Course" in user dashboard
driver.find_element_by_xpath("html/body/main/section[3]/div/a[1]").click()

CSS Selector Locator

More concise and faster than XPath in many browsers, using CSS-style selectors.

  1. Class Selector: Prefix with .; chain multiple segments for compound classes.
  2. ID Selector: Prefix with #.
  3. Attribute Selector: Enclose single/multiple attributes in []; quotes around values are optional but recommended for special characters.
  4. Combination Selector: Mix tag, ID, class, and attribute checks.
<!-- Example login button HTML -->
<input class="submit-btn login-btn" type="submit" onclick="return Auth.validate()" value="Access Account">
# Click login via compound class selector
driver.find_element_by_css_selector(".submit-btn.login-btn").click()
# Click login via attribute selector
driver.find_element_by_css_selector("[onclick='return Auth.validate()']").click()

Unified By Class Locator

A consistent syntax that takes two arguments: a By constant and the locator value. This requires importing By first.

from selenium.webdriver.common.by import By

# Equivalent locators using the By class
driver.find_element(By.ID, "user_login")
driver.find_element(By.NAME, "school_level")
driver.find_element(By.CLASS_NAME, "btn-secondary")
driver.find_element(By.TAG_NAME, "input")
driver.find_element(By.LINK_TEXT, "Sign Out")
driver.find_element(By.PARTIAL_LINK_TEXT, "Sign")
driver.find_element(By.XPATH, ".//div[@id='auth_panel']/button[2]")
driver.find_element(By.CSS_SELECTOR, "[onclick='return Auth.validate()']")

Tags: Selenium WebDriver UI Automation Element Locators Python Selenium

Posted on Fri, 08 May 2026 11:24:10 +0000 by patsfans