Bird
Raised Fist0
Selenium Pythontesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using the Page Factory pattern in Selenium with Python?
easy
A. To generate test reports automatically
B. To execute tests faster by parallel processing
C. To group web elements in a class for easier access and maintenance
D. To handle browser pop-ups and alerts

Solution

  1. Step 1: Understand the role of Page Factory

    Page Factory helps organize web elements by grouping them in a class, making tests easier to read and maintain.
  2. Step 2: Compare with other options

    Options B, C, and D describe different Selenium features unrelated to Page Factory's main purpose.
  3. Final Answer:

    To group web elements in a class for easier access and maintenance -> Option C
  4. Quick Check:

    Page Factory = Group elements for easy access [OK]
Hint: Page Factory groups elements in classes for neat tests [OK]
Common Mistakes:
  • Confusing Page Factory with test execution speed
  • Thinking it generates reports
  • Assuming it handles browser alerts
2. Which of the following is the correct way to declare a web element using Page Factory in Selenium Python?
easy
A. element = driver.find_element(By.ID, 'username')
B. username = (By.ID, 'username')
C. username = self.driver.find_element_by_id('username')
D. username = self.driver.find_element(By.ID, 'username')

Solution

  1. Step 1: Identify Page Factory syntax in Python

    Page Factory uses class attributes with locator tuples like (By.ID, 'username') to declare elements before initializing them.
  2. Step 2: Check each option

    username = (By.ID, 'username') shows the correct locator tuple usage, which is standard in Page Factory.
  3. Final Answer:

    username = (By.ID, 'username') -> Option B
  4. Quick Check:

    Page Factory uses locator tuples (By.xx, 'value') [OK]
Hint: Declare Page Factory elements as (By.xx, 'value') tuples [OK]
Common Mistakes:
  • Using direct driver.find_element calls instead of Page Factory declarations
  • Missing the locator tuple syntax
  • Assigning elements directly without initialization
3. Given the following code snippet using Page Factory in Selenium Python, what will be the output of print(page.login_button.is_displayed()) if the button is visible on the page?
from selenium.webdriver.common.by import By
from selenium.webdriver.support.page_factory import PageFactory

class LoginPage:
    def __init__(self, driver):
        self.driver = driver
        PageFactory.init_elements(driver, self)

    login_button = (By.ID, 'loginBtn')

page = LoginPage(driver)
print(page.login_button.is_displayed())
medium
A. True
B. False
C. AttributeError
D. None

Solution

  1. Step 1: Understand Page Factory element initialization

    PageFactory.init_elements initializes elements so login_button refers to the actual web element.
  2. Step 2: Check the method call

    If the button is visible, is_displayed() returns True, so printing it outputs True.
  3. Final Answer:

    True -> Option A
  4. Quick Check:

    Visible element's is_displayed() = True [OK]
Hint: is_displayed() returns True if element is visible [OK]
Common Mistakes:
  • Expecting None because locator needs initialization
  • Confusing AttributeError due to wrong initialization
  • Assuming is_displayed() returns string
4. Identify the error in the following Page Factory code snippet and choose the correct fix:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.page_factory import PageFactory

class HomePage:
    def __init__(self, driver):
        self.driver = driver
        PageFactory.init_elements(self, self.driver)

    submit_button = (By.XPATH, "//button[@id='submit']")

page = HomePage(driver)
page.submit_button.click()
medium
A. Initialize elements with PageFactory.init_elements(driver, self)
B. Replace @FindBy with @find_by (lowercase)
C. Import FindBy from correct module and use it as decorator
D. Import and use find_element instead of PageFactory

Solution

  1. Step 1: Check PageFactory.init_elements arguments

    The code uses PageFactory.init_elements(self, self.driver) but arguments must be driver first, then self.
  2. Step 2: Confirm correct usage

    Using PageFactory.init_elements(driver, self) fixes the error and allows proper element initialization.
  3. Final Answer:

    Initialize elements with PageFactory.init_elements(driver, self) -> Option A
  4. Quick Check:

    init_elements(driver, self) order [OK]
Hint: PageFactory.init_elements(driver, self) - driver first [OK]
Common Mistakes:
  • Wrong argument order for PageFactory.init_elements
  • Using wrong decorator name case
  • Incorrect PageFactory.init_elements parameters
5. You want to create a Page Factory class for a login page with username, password fields, and a login button. Which approach correctly applies the Page Factory pattern to initialize and use these elements in Selenium Python?
hard
A. Write all element locators inside test functions without any class or decorator
B. Use driver.find_element calls inside each test method without class grouping
C. Create global variables for elements outside any class and access them directly
D. Declare elements with @FindBy decorators, initialize with PageFactory.init_elements, then use element methods

Solution

  1. Step 1: Understand Page Factory best practice

    Page Factory groups elements in a class using locator tuples and initializes them with PageFactory.init_elements.
  2. Step 2: Compare with other options

    Options B, C, and D do not use Page Factory pattern and lead to less maintainable code.
  3. Final Answer:

    Declare elements with @FindBy decorators, initialize with PageFactory.init_elements, then use element methods -> Option D
  4. Quick Check:

    Page Factory = locators + init_elements + class grouping [OK]
Hint: Use locators and init_elements to organize page elements [OK]
Common Mistakes:
  • Skipping class and locators for elements
  • Using driver calls directly in tests
  • Not initializing elements with PageFactory
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard