The Page Factory pattern helps organize web page elements in tests. It makes tests easier to read and maintain by grouping element locators and actions in one place.
Page factory pattern in Selenium Python
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(self.driver, self) username = (By.ID, 'username') password = (By.ID, 'password') login_button = (By.ID, 'loginBtn')
Use By to specify how to find elements (ID, NAME, XPATH, etc.).
Page Factory groups element locators inside a class representing the page.
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.driver, self) search_box = (By.NAME, 'q') search_button = (By.NAME, 'btnK')
from selenium.webdriver.common.by import By from selenium.webdriver.support.page_factory import PageFactory class ContactPage: def __init__(self, driver): self.driver = driver PageFactory.init_elements(self.driver, self) email_field = (By.XPATH, '//input[@type="email"]') submit_button = (By.CSS_SELECTOR, 'button.submit')
This test script opens a login page, uses the Page Factory pattern to find elements, and performs a login action. It prints a confirmation message after trying to log in.
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.page_factory import PageFactory import time class LoginPage: def __init__(self, driver): self.driver = driver PageFactory.init_elements(self.driver, self) username = (By.ID, 'username') password = (By.ID, 'password') login_button = (By.ID, 'loginBtn') def login(self, user, pwd): self.driver.find_element(*self.username).send_keys(user) self.driver.find_element(*self.password).send_keys(pwd) self.driver.find_element(*self.login_button).click() # Setup WebDriver (example with Chrome) driver = webdriver.Chrome() driver.get('https://example.com/login') login_page = LoginPage(driver) login_page.login('testuser', 'testpass') # Wait to see the result time.sleep(2) print('Login attempted') driver.quit()
Always initialize elements inside the page class constructor.
Use clear and descriptive names for element variables.
Page Factory helps keep tests clean but does not replace good test design.
Page Factory groups web elements in a class for easy access.
It makes tests easier to read and maintain.
Use it to reduce duplicate code and improve test organization.
Practice
Page Factory pattern in Selenium with Python?Solution
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.Step 2: Compare with other options
Options B, C, and D describe different Selenium features unrelated to Page Factory's main purpose.Final Answer:
To group web elements in a class for easier access and maintenance -> Option CQuick Check:
Page Factory = Group elements for easy access [OK]
- Confusing Page Factory with test execution speed
- Thinking it generates reports
- Assuming it handles browser alerts
Solution
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.Step 2: Check each option
username = (By.ID, 'username')shows the correct locator tuple usage, which is standard in Page Factory.Final Answer:
username = (By.ID, 'username') -> Option BQuick Check:
Page Factory uses locator tuples (By.xx, 'value') [OK]
- Using direct driver.find_element calls instead of Page Factory declarations
- Missing the locator tuple syntax
- Assigning elements directly without initialization
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())Solution
Step 1: Understand Page Factory element initialization
PageFactory.init_elements initializes elements sologin_buttonrefers 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 AQuick Check:
Visible element's is_displayed() = True [OK]
- Expecting None because locator needs initialization
- Confusing AttributeError due to wrong initialization
- Assuming is_displayed() returns string
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()Solution
Step 1: Check PageFactory.init_elements arguments
The code usesPageFactory.init_elements(self, self.driver)but arguments must be driver first, then self.Step 2: Confirm correct usage
UsingPageFactory.init_elements(driver, self)fixes the error and allows proper element initialization.Final Answer:
Initialize elements withPageFactory.init_elements(driver, self)-> Option AQuick Check:
init_elements(driver, self) order [OK]
- Wrong argument order for PageFactory.init_elements
- Using wrong decorator name case
- Incorrect PageFactory.init_elements parameters
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 DQuick Check:
Page Factory = locators + init_elements + class grouping [OK]
- Skipping class and locators for elements
- Using driver calls directly in tests
- Not initializing elements with PageFactory
Ready to prove it?
Pass the test and this pattern moves unknown
