We use element locators in a page class to find parts of a web page easily and clearly. This helps us write tests that are simple and organized.
Element locators in page class in Selenium Python
class PageClassName: def __init__(self, driver): self.driver = driver self.element_name = (By.LOCATOR_TYPE, 'locator_value')
Use tuples with By to define locators clearly.
Keep locators as instance variables to reuse them easily.
from selenium.webdriver.common.by import By class LoginPage: def __init__(self, driver): self.driver = driver self.username_input = (By.ID, 'username') self.password_input = (By.NAME, 'password') self.login_button = (By.CSS_SELECTOR, 'button.login')
from selenium.webdriver.common.by import By class HomePage: def __init__(self, driver): self.driver = driver self.search_box = (By.XPATH, '//input[@type="search"]') self.submit_button = (By.CLASS_NAME, 'submit-btn')
This test script uses a page class with element locators to enter username and password, then click login. It prints a success message if no errors occur.
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options class LoginPage: def __init__(self, driver): self.driver = driver self.username_input = (By.ID, 'username') self.password_input = (By.NAME, 'password') self.login_button = (By.CSS_SELECTOR, 'button.radius') def enter_username(self, username): self.driver.find_element(*self.username_input).send_keys(username) def enter_password(self, password): self.driver.find_element(*self.password_input).send_keys(password) def click_login(self): self.driver.find_element(*self.login_button).click() # Setup Chrome driver (assuming chromedriver is in PATH) options = Options() options.add_argument('--headless') # Run browser in headless mode service = Service() driver = webdriver.Chrome(service=service, options=options) try: driver.get('https://the-internet.herokuapp.com/login') login_page = LoginPage(driver) login_page.enter_username('testuser') login_page.enter_password('password123') login_page.click_login() print('Test passed: Login actions performed successfully') except Exception as e: print(f'Test failed: {e}') finally: driver.quit()
Use the * operator to unpack locator tuples when calling find_element.
Keep locators in one place to make maintenance easier if the page changes.
Use meaningful variable names for locators to understand what element they represent.
Element locators in a page class help find web elements clearly and reuse them.
Locators are stored as tuples with By strategies and values.
This approach keeps test code clean, simple, and easy to maintain.
Practice
Solution
Step 1: Understand the role of element locators
Element locators help identify web elements on a page so tests can interact with them.Step 2: Recognize the benefit of using a page class
Storing locators in a page class allows easy reuse and keeps code clean.Final Answer:
To find web elements clearly and reuse them easily -> Option AQuick Check:
Element locators = reuse and clarity [OK]
- Confusing locators with test data storage
- Thinking page class runs browser commands
- Assuming locators hold test logic
Solution
Step 1: Recall the correct locator tuple format
Locators are tuples with By strategy and string value, e.g., (By.ID, 'value').Step 2: Check each option's syntax
submit_button = (By.ID, 'submit') uses (By.ID, 'submit'), which is correct. Others use wrong syntax or missing quotes.Final Answer:
submit_button = (By.ID, 'submit') -> Option DQuick Check:
Locator tuple = (By.ID, 'value') [OK]
- Using By.ID('submit') instead of tuple
- Missing quotes around 'submit'
- Using wrong tuple structure
login_button = (By.CSS_SELECTOR, 'button.login') in a page class, what will driver.find_element(*login_button) do?Solution
Step 1: Understand the locator tuple and unpacking
The locator is a tuple (By.CSS_SELECTOR, 'button.login'). Using * unpacks it into two arguments.Step 2: Know what find_element does
find_element returns the first matching element, not all elements.Final Answer:
Find the first button element with class 'login' on the page -> Option BQuick Check:
find_element + CSS selector = first match [OK]
- Confusing find_element with find_elements
- Thinking unpacking causes syntax error
- Misunderstanding CSS selector as tag name
search_box = (By.NAME, 'q'). Your test code uses driver.find_element(search_box) and fails. What is the error and how to fix it?Solution
Step 1: Identify the cause of failure
driver.find_element expects two arguments, but passing a tuple without unpacking sends one argument.Step 2: Apply the correct fix
Use the unpacking operator * to pass locator as separate arguments: driver.find_element(*search_box).Final Answer:
Error: Missing unpacking operator '*'. Fix: Use driver.find_element(*search_box) -> Option AQuick Check:
Unpack locator tuple with * [OK]
- Not using * causes TypeError
- Changing locator type unnecessarily
- Assuming locator value is wrong
<a> tag or a <button> tag. Which locator is best to find it reliably?Solution
Step 1: Understand the locator requirements
The element can be either an <a> or <button> with text 'Home', so locator must cover both tags.Step 2: Evaluate locator options
home_link = (By.XPATH, "//a[text()='Home'] | //button[text()='Home']") uses XPath with OR (|) to match either tag by exact text, which is reliable. home_link = (By.LINK_TEXT, 'Home') only works for <a> tags. home_link = (By.CSS_SELECTOR, 'a.home, button.home') uses classes which may not exist. home_link = (By.ID, 'home') uses ID which is not given.Final Answer:
home_link = (By.XPATH, "//a[text()='Home'] | //button[text()='Home']") -> Option CQuick Check:
XPath OR matches multiple tags [OK]
- Using LINK_TEXT misses button tags
- Using CSS selectors without classes
- Assuming ID exists without confirmation
Ready to prove it?
Pass the test and this pattern moves unknown
