Course → 1 · Selenium automation → Finding Elements
1 · Selenium automation
Locators: ID, CSS, and XPath
12 min · Module 7
Prefer stable attributes such as id and data-testid. CSS selectors are readable; XPath is powerful for complex DOM trees.
By.ID— fastest when IDs are uniqueBy.CSS_SELECTOR— great for classes and attributesBy.XPATH— useful for text-based lookups
The login lab exposes username, password, and login-button IDs.
Example / notes
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("http://127.0.0.1:8000/lab/login/")
user = driver.find_element(By.ID, "username")
pwd = driver.find_element(By.CSS_SELECTOR, "[data-testid='password']")
btn = driver.find_element(By.XPATH, "//button[@id='login-button']")
print(user.tag_name, pwd.get_attribute("type"), btn.text)
driver.quit()
Practice in Lab: /lab/login/
Modules in this track
- 6. Getting Started
- 7. Finding Elements
- 8. Interactions
- 9. Waits
- 10. Assertions & Structure
- 11. Page Object Model
- 12. Alerts, Frames & Windows
- 13. Headless & Next Steps