Relative Locators and Choosing the Best Locator Strategy

Selenium 13 min min read Updated: Nov 16, 2026 Intermediate
Relative Locators and Choosing the Best Locator Strategy
Intermediate Topic 5 of 7

Relative Locators (Selenium 4)

Relative locators find an element by describing where it is compared to another element, as a person would: "the input below the Email label".

python from selenium.webdriver.support.relative_locator import locate_with from selenium.webdriver.common.by import By email_label = driver.find_element(By.ID, "email-label") # the input that is below the label email_input = driver.find_element(locate_with(By.TAG_NAME, "input").below(email_label)) email_input.send_keys("ana@example.com")
MethodMeaning
.above(el)Element is above el
.below(el)Element is below el
.to_left_of(el)Element is to the left
.to_right_of(el)Element is to the right
.near(el)Within about 50 pixels. You can pass a distance
python password = driver.find_element(By.ID, "password") remember = driver.find_element(locate_with(By.CSS_SELECTOR, "input[type='checkbox']").below(password)) # combine btn = driver.find_element( locate_with(By.TAG_NAME, "button").to_right_of(cancel_btn).near(password, 200))
How it works: Selenium reads the position of elements on screen using JavaScript and returns the match. It depends on layout, so results can change on small screens. Use it as a helper, not as your first choice.

Choosing a Locator Strategy

A test that fails because a locator changed is a maintenance cost. Choose locators that survive redesigns.

RankLocatorStability
1data-testid / data-test / data-qa attributesExcellent: added just for tests
2id (not auto-generated)Very good
3name, aria-label, placeholderGood
4Short CSS using meaningful classesGood
5XPath with text or a stable neighborMedium
6Relative locatorsMedium (layout dependent)
7Absolute XPath, long chains, indexes like div[3]/div[2]Poor. Avoid

Practice Site Example

The practice shop saucedemo.com adds data-test attributes:

python driver.get("https://www.saucedemo.com") driver.find_element(By.CSS_SELECTOR, "[data-test='username']").send_keys("standard_user") driver.find_element(By.CSS_SELECTOR, "[data-test='password']").send_keys("secret_sauce") driver.find_element(By.CSS_SELECTOR, "[data-test='login-button']").click()

Keep Locators in One Place

python class LoginLocators: USERNAME = (By.CSS_SELECTOR, "[data-test='username']") PASSWORD = (By.CSS_SELECTOR, "[data-test='password']") SUBMIT = (By.CSS_SELECTOR, "[data-test='login-button']") driver.find_element(*LoginLocators.USERNAME).send_keys("standard_user")

The * unpacks the tuple into the two arguments. When the page changes you fix one line. This idea becomes the Page Object Model later.

Checklist Before You Choose

  • Is it unique? (1 match in DevTools)
  • Is it stable? (not a number or random string)
  • Is it readable? (another tester understands it)
  • Is it independent of layout? (no long paths)
  • Could a developer add a data-testid to make it perfect?
Talk to developers: Asking for test attributes is normal and makes automation 10 times more reliable.

Conclusion

Relative locators describe position, but stable attributes are always better. Prefer data-testid, then id and name, then short CSS or XPath, and store locators in one place.

Quick Summary: data-testid > id > name > CSS > XPath > relative > absolute paths.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators