An element locator is a way to find and select elements on a web page, like buttons or text fields, so you can interact with them in your tests.
Click to reveal answer
beginner
Why do we use a page class in Selenium tests?
A page class groups all element locators and actions for one web page in one place. It makes tests easier to read and maintain, like having a map for that page.
Click to reveal answer
beginner
Name four common locator strategies in Selenium.
Common locator strategies are: ID, Name, CSS Selector, and XPath. Each helps find elements differently, like using an address or a description.
Click to reveal answer
intermediate
What is the best practice for choosing locators in a page class?
Choose locators that are unique, stable, and simple. For example, prefer ID over XPath because IDs are usually unique and less likely to change.
Click to reveal answer
intermediate
How does using a page class improve test maintenance?
If a page changes, you only update locators in one place—the page class—instead of many test files. This saves time and reduces errors.
Click to reveal answer
Which locator is usually the most reliable to use in Selenium?
AClass Name
BXPath
CID
DTag Name
✗ Incorrect
ID is usually unique and stable, making it the most reliable locator.
What is the main purpose of a page class in Selenium tests?
ATo group locators and actions for a web page
BTo run tests faster
CTo store test data
DTo generate reports
✗ Incorrect
A page class organizes locators and actions for one page, improving test clarity and maintenance.
Which locator strategy uses a path-like syntax to find elements?
ACSS Selector
BXPath
CName
DID
✗ Incorrect
XPath uses a path syntax to navigate the HTML structure.
If a locator changes on the web page, where should you update it?
AIn the test runner
BIn every test file
CIn the browser settings
DIn the page class
✗ Incorrect
Updating locators in the page class keeps tests consistent and easier to maintain.
Which of these is NOT a good locator choice?
ARandomly generated class name
BStable CSS Selector
CName attribute
DUnique ID
✗ Incorrect
Random class names can change often, making tests fragile.
Explain how element locators and page classes work together in Selenium tests.
Think about how you find things on a map and keep the map updated.
You got /4 concepts.
Describe best practices for choosing element locators in a page class.
Imagine picking the easiest and most reliable way to find a friend in a crowd.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using element locators in a page class in Selenium Python?
easy
A. To find web elements clearly and reuse them easily
B. To write complex test logic inside the page class
C. To store test data separately from the test scripts
D. To execute browser commands directly
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 A
Quick Check:
Element locators = reuse and clarity [OK]
Hint: Locators in page class = find elements + reuse [OK]
Common Mistakes:
Confusing locators with test data storage
Thinking page class runs browser commands
Assuming locators hold test logic
2. Which of the following is the correct way to define a locator for a button with id 'submit' in a Selenium page class?
easy
A. submit_button = By('id', 'submit')
B. submit_button = By.ID('submit')
C. submit_button = ('id', submit)
D. submit_button = (By.ID, 'submit')
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.
3. Given the locator login_button = (By.CSS_SELECTOR, 'button.login') in a page class, what will driver.find_element(*login_button) do?
medium
A. Cause a syntax error because of unpacking
B. Find the first button element with class 'login' on the page
C. Find all button elements with class 'login' on the page
D. Find an element by tag name 'button.login'
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 B
Quick Check:
find_element + CSS selector = first match [OK]
Hint: find_element finds first match, unpack locator tuple [OK]
Common Mistakes:
Confusing find_element with find_elements
Thinking unpacking causes syntax error
Misunderstanding CSS selector as tag name
4. You have this locator in your page class: 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?
medium
A. Error: Missing unpacking operator '*'. Fix: Use driver.find_element(*search_box)
B. Error: Wrong locator type. Fix: Change By.NAME to By.ID
C. Error: Locator value is incorrect. Fix: Change 'q' to 'search'
D. Error: find_element cannot use tuples. Fix: Use string locator only
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 A
Quick Check:
Unpack locator tuple with * [OK]
Hint: Always unpack locator tuple with * in find_element [OK]
Common Mistakes:
Not using * causes TypeError
Changing locator type unnecessarily
Assuming locator value is wrong
5. You want to create a page class locator for a link with text 'Home' that may appear as either an <a> tag or a <button> tag. Which locator is best to find it reliably?
hard
A. home_link = (By.LINK_TEXT, 'Home')
B. home_link = (By.CSS_SELECTOR, 'a.home, button.home')
C. home_link = (By.XPATH, "//a[text()='Home'] | //button[text()='Home']")
D. home_link = (By.ID, 'home')
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 C
Quick Check:
XPath OR matches multiple tags [OK]
Hint: Use XPath OR (|) to match multiple tag types [OK]
Common Mistakes:
Using LINK_TEXT misses button tags
Using CSS selectors without classes
Assuming ID exists without confirmation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.