SeleniumLab

Course1 · 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.

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/

Log in to track progress / take quizzes
find_element vs find_elements →
Modules in this track