0
0
Selenium Pythontesting~5 mins

Page factory pattern in Selenium Python

Choose your learning style9 modes available
Introduction

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.

When you want to keep your test code clean and organized.
When you have many web elements to interact with on a page.
When you want to reuse page elements and actions across multiple tests.
When you want to reduce duplicate code for locating elements.
When you want your tests to be easier to update if the web page changes.
Syntax
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.

Examples
This example shows locating elements by their name attribute.
Selenium Python
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')
This example uses XPath and CSS selectors to find elements.
Selenium Python
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')
Sample Program

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.

Selenium Python
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()
OutputSuccess
Important Notes

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.

Summary

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.