0
0
Selenium Pythontesting~20 mins

Page factory pattern in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Page Factory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of element initialization in Page Factory
Consider the following Python Selenium code using the Page Factory pattern. What will be the output when accessing the login_button element's tag name?

Assume the page contains a button with tag name button and id loginBtn.
Selenium Python
from selenium.webdriver.common.by import By

class LoginPage:
    def __init__(self, driver):
        self.driver = driver
        self.login_button = driver.find_element(By.ID, 'loginBtn')

# Mock driver and element for demonstration
class MockElement:
    def __init__(self, tag_name):
        self.tag_name = tag_name

class MockDriver:
    def find_element(self, by, value):
        if by == By.ID and value == 'loginBtn':
            return MockElement('button')
        raise Exception('Element not found')

page = LoginPage(MockDriver())
print(page.login_button.tag_name)
Aspan
Binput
Cdiv
Dbutton
Attempts:
2 left
💡 Hint
Think about what tag name the mock element is initialized with.
assertion
intermediate
1:30remaining
Correct assertion for Page Factory element visibility
You have a Page Factory element submit_btn defined as:
submit_btn = driver.find_element(By.ID, 'submit')

Which assertion correctly verifies that this button is visible on the page?
Aassert submit_btn.is_displayed() == True
Bassert submit_btn.is_enabled() == True
Cassert submit_btn.text == 'Submit'
Dassert submit_btn is not None
Attempts:
2 left
💡 Hint
Visibility means the element is shown on the page, not just present or enabled.
🔧 Debug
advanced
2:30remaining
Identify the error in Page Factory element locator
Examine the following Page Factory element locator code snippet. What error will occur when running this code?
from selenium.webdriver.common.by import By

class HomePage:
    def __init__(self, driver):
        self.driver = driver
        self.search_box = driver.find_element(By.CSS_SELECTOR, '#searchBox')
        self.submit_btn = driver.find_element(By.ID, 'submitBtn')

    def search(self, text):
        self.search_box.send_keys(text)
        self.submit_btn.click()
ANo error, code runs fine
BNoSuchElementException if elements not found
CAttributeError because 'driver' is None
DTypeError due to wrong locator syntax
Attempts:
2 left
💡 Hint
Think about what happens if the elements are not present on the page.
framework
advanced
2:00remaining
Best practice for Page Factory element initialization
Which of the following is the best practice to initialize Page Factory elements in Selenium with Python to improve code readability and maintainability?
AUse <code>@property</code> decorators to locate elements dynamically
BInitialize elements inside each test method separately
CLocate all elements once in the constructor and store as instance variables
DUse global variables for element locators
Attempts:
2 left
💡 Hint
Dynamic locating helps avoid stale element references and improves clarity.
🧠 Conceptual
expert
3:00remaining
Why use Page Factory pattern in Selenium tests?
Which of the following best explains the main advantage of using the Page Factory pattern in Selenium test automation?
AIt automatically retries failed tests to improve stability
BIt replaces the need for explicit waits in tests
CIt separates test logic from page element locators for better code organization
DIt speeds up test execution by caching all elements at once
Attempts:
2 left
💡 Hint
Think about how Page Factory helps manage code structure.