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.
- Absolute Path: Traverses the entire DOM from the root (
html), but breaks easily with layout changes. - Attribute-Based: Uses any unique element attribute;
*matches any tag. - Hierarchy + Attribute: Combines parent/child relationships with attributes for precision.
- Logical Operators: Uses
and/orto 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.
- Class Selector: Prefix with
.; chain multiple segments for compound classes. - ID Selector: Prefix with
#. - Attribute Selector: Enclose single/multiple attributes in
[]; quotes around values are optional but recommended for special characters. - 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()']")