Element locators in page class in Selenium Python - Build an Automation Script
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC class LoginPage: # Element locators stored as class variables LOGIN_BUTTON = (By.ID, 'login-button') def __init__(self, driver): self.driver = driver def is_login_button_visible(self): wait = WebDriverWait(self.driver, 10) login_button = wait.until(EC.visibility_of_element_located(self.LOGIN_BUTTON)) return login_button.is_displayed() def test_login_button_visibility(): driver = webdriver.Chrome() driver.get('https://example.com/login') login_page = LoginPage(driver) assert login_page.is_login_button_visible(), 'Login button should be visible on login page' driver.quit()
The LoginPage class stores the login button locator as a class variable LOGIN_BUTTON using By.ID. This keeps locators organized and easy to maintain.
The method is_login_button_visible uses an explicit wait to wait up to 10 seconds for the login button to be visible. This avoids timing issues.
The test function test_login_button_visibility opens the browser, navigates to the login page, creates a LoginPage object, and asserts the login button is visible. Finally, it closes the browser.
This structure follows best practices by separating locators in a page class and using explicit waits and clear assertions.
Now add data-driven testing with 3 different login page URLs to verify the login button visibility on each
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
