Page Factory is a design pattern in Selenium that helps initialize web elements easily using annotations, making tests cleaner and easier to maintain.
Click to reveal answer
beginner
How do you declare a web element using Page Factory in Python?
You define locators as class attributes using tuples with By, and use the @property decorator to lazily locate and return elements when first accessed.
Click to reveal answer
intermediate
Why is Page Factory pattern preferred over traditional element locating?
Because it reduces code duplication, improves readability, and initializes elements only when they are used, which can improve test performance.
Click to reveal answer
intermediate
What is the role of lazy initialization in Page Factory?
It initializes web elements lazily when accessed so they can be used directly without calling find_element repeatedly each time.
Click to reveal answer
beginner
Give a simple example of a Page Factory class in Selenium Python.
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
Step 1: Understand Page Factory element initialization
PageFactory.init_elements initializes elements so login_button refers to the actual web element.
Step 2: Check the method call
If the button is visible, is_displayed() returns True, so printing it outputs True.
Final Answer:
True -> Option A
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
Step 1: Check PageFactory.init_elements arguments
The code uses PageFactory.init_elements(self, self.driver) but arguments must be driver first, then self.
Step 2: Confirm correct usage
Using PageFactory.init_elements(driver, self) fixes the error and allows proper element initialization.
Final Answer:
Initialize elements with PageFactory.init_elements(driver, self) -> Option A
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
Step 1: Understand Page Factory best practice
Page Factory groups elements in a class using locator tuples and initializes them with PageFactory.init_elements.
Step 2: Compare with other options
Options B, C, and D do not use Page Factory pattern and lead to less maintainable code.
Final Answer:
Declare elements with @FindBy decorators, initialize with PageFactory.init_elements, then use element methods -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.