header_logo
Post thumbnail
SELENIUM

Page Object Model (POM) Design Pattern with Python Selenium: A Complete Guide

By Basil Ahamed

In Selenium automation, as your test suite grows, maintaining scripts becomes challenging. Code duplication, fragile locators, and poor readability often slow down your team. That’s where the Page Object Model (POM) design pattern comes in.

In this guide, we’ll explore how to implement Page Object Model in Python Selenium, why it’s essential, and how it can improve your automation framework’s scalability, maintainability, and readability.

Table of contents


  1. What is Page Object Model (POM)?
    • Key Benefits of POM:
  2. Folder Structure for POM
  3. Implementing POM in Python Selenium
    • Step 1: Install Required Packages
    • Step 2: Driver Setup (driver_factory.py)
    • Step 3: Create Page Classes
    • Step 4: Write Test Script (test_login.py)
    • Step 5: Running the Tests
    • Step 6: Advanced Enhancements
  4. Best Practices for POM in Python Selenium
  5. Conclusion

What is Page Object Model (POM)?

What is Page Object Model (POM)?

Page Object Model (POM) is a design pattern that creates an object repository for web UI elements. Each web page is represented as a class, and its elements are defined as variables. All possible interactions (like click, input, and get text) are implemented as methods within that class.

Key Benefits of POM:

  • Separation of Concerns: Keeps locators and actions separate from test logic.
  • Improved Maintainability: Update locators in one place.
  • Code Reusability: Common actions can be reused across tests.
  • Better Readability: Tests are more readable and closer to business logic.

Folder Structure for POM 

A clean directory structure is crucial for POM. Here’s a typical structure:

📦 project-root/

├── 📁 tests/

  ├── 📝 __ init __.py                 # Empty init file

│   └── 📝 test_login.py                # Test cases using Page Objects

├── 📁 pages/

  ├── 📝 __ init __.py                 # Empty init file

│   ├── 📝 login_page.py              # Page Object for Login Page

│   └── 📝 home_page.py             # Page Object for Home Page

├── 📁 utils/

  ├── 📝 __ init __.py                 # Empty init file

│   ├── 📝 driver_factory.py           # Browser driver setup & teardown

│   └── 📝 config.py                       # Configurations (URLs, credentials)

├── 📁 reports/

│   ├── 📁 pytest_html/                  # HTML report results

│   └── 📁 screenshots/                 # Screenshots for failed tests

└── 📝 requirements.txt                 # Python dependencies

Implementing POM in Python Selenium

Implementing POM in Python Selenium

Let’s walk through an example of automating Amazon Login using POM.

Step 1: Install Required Packages

pip install selenium pytest

Step 2: Driver Setup (driver_factory.py)

from selenium import webdriver

class DriverFactory:

    @staticmethod

    def get_driver():

        driver = webdriver.Chrome()

        driver.maximize_window()

        driver.implicitly_wait(10)

        return driver
MDN

Step 3: Create Page Classes

login_page.py

from selenium.webdriver.common.by import By

class LoginPage:

    def __init__(self, driver):

        self.driver = driver

        self.sign_in_link = (By.ID, "nav-link-accountList")

        self.email_field = (By.ID, "ap_email")

        self.continue_button = (By.ID, "continue")

    def click_sign_in(self):

        self.driver.find_element(*self.sign_in_link).click()

    def enter_email(self, email):

        self.driver.find_element(*self.email_field).send_keys(email)

    def click_continue(self):

        self.driver.find_element(*self.continue_button).click()

Step 4: Write Test Script (test_login.py)

import pytest

from pages.login_page import LoginPage

from utils.driver_factory import DriverFactory

class TestLogin:

    def setup_method(self):

        self.driver = DriverFactory.get_driver()

        self.driver.get("https://www.amazon.com")

    def teardown_method(self):

        self.driver.quit()

    def test_login_with_valid_email(self):

        login_page = LoginPage(self.driver)

        login_page.click_sign_in()

        login_page.enter_email("testuser@example.com")

        login_page.click_continue()

        assert "Authentication" in self.driver.title  # Example assertion

Step 5: Running the Tests

pytest tests/test_login.py

Step 6: Advanced Enhancements

  • BasePage Class: For common actions like click(), send_keys(), get_text().
  • PageFactory Pattern: To lazily initialize web elements.
  • Logging & Reporting: Integrate Pytest HTML Reports.
  • Environment Config: Manage URLs, credentials via config files.

Best Practices for POM in Python Selenium

Best Practices for POM in Python Selenium
  • Follow SRP (Single Responsibility Principle) — one page, one class.
  • Avoid hardcoding locators and data.
  • Implement proper waits instead of time.sleep().
  • Modularize common utilities (driver setup, waits, screenshots).
  • Keep test assertions in test scripts, not in page objects.

In case you want to learn more about Python Selenium in Automation Testing, consider enrolling for GUVI’s Selenium Automation Testing Course, which teaches you everything from scratch by providing an industry-grade certificate!

MDN

Conclusion

The Page Object Model is a must-have design pattern for any robust Selenium automation framework. It helps you write clean, maintainable, and scalable test code in Python.

By adopting POM, you not only reduce technical debt but also create a foundation for advanced practices like Data-Driven Testing, CI/CD Integration, and Parallel Execution.

Success Stories

Did you enjoy this article?

Schedule 1:1 free counselling

Similar Articles

Loading...
Share logo Copy link
Power Packed Webinars
Free Webinar Icon
Power Packed Webinars
Subscribe now for FREE! 🔔
close
Webinar ad
Table of contents Table of contents
Table of contents Articles
Close button

  1. What is Page Object Model (POM)?
    • Key Benefits of POM:
  2. Folder Structure for POM
  3. Implementing POM in Python Selenium
    • Step 1: Install Required Packages
    • Step 2: Driver Setup (driver_factory.py)
    • Step 3: Create Page Classes
    • Step 4: Write Test Script (test_login.py)
    • Step 5: Running the Tests
    • Step 6: Advanced Enhancements
  4. Best Practices for POM in Python Selenium
  5. Conclusion