Using Page Object Model (POM) with Python Selenium

 The Page Object Model (POM) is a design pattern in Selenium automation that enhances test maintenance and reduces code duplication. It promotes better organization of code by separating locators and test logic. In POM, each web page is represented as a class, and the elements and actions on the page are defined as methods.

✅ Why Use Page Object Model?

Improves readability and maintainability of test scripts

Reduces code duplication

Makes test automation scalable

Helps manage locators efficiently

🧱 Structure of POM in Python

A typical POM structure has:

project/

├── pages/

│   ├── login_page.py

│   ├── home_page.py

├── tests/

│   └── test_login.py

├── conftest.py (for setup and teardown)

└── utils/ (for common utilities)

🔧 Step-by-Step Example

Let’s automate a login scenario using POM with Selenium and Python.

1. Install Selenium

pip install selenium

2. Login Page – pages/login_page.py

from selenium.webdriver.common.by import By

class LoginPage:

    def __init__(self, driver):

        self.driver = driver

        self.username_input = (By.ID, "username")

        self.password_input = (By.ID, "password")

        self.login_button = (By.ID, "loginBtn")

    def enter_username(self, username):

        self.driver.find_element(*self.username_input).send_keys(username)

    def enter_password(self, password):

        self.driver.find_element(*self.password_input).send_keys(password)

    def click_login(self):

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

3. Test File – tests/test_login.py

from selenium import webdriver

from pages.login_page import LoginPage

import time

def test_valid_login():

    driver = webdriver.Chrome()

    driver.get("https://example.com/login")

    login = LoginPage(driver)

    login.enter_username("testuser")

    login.enter_password("password123")

    login.click_login()

    time.sleep(3)

    assert "Dashboard" in driver.title

    driver.quit()

💡 Best Practices

Use pytest or unittest for test structure.

Store element locators as class variables.

Add wait conditions using WebDriverWait for stability.

Create a BasePage class for reusable functions (e.g., click, type, wait_for_element).

📌 Conclusion

Using Page Object Model with Python Selenium helps create clean, scalable, and maintainable test automation scripts. By organizing code into logical page objects, you not only save time but also simplify debugging and future updates.

POM is an essential design pattern for any serious automation framework—learn it once, and it will serve you well across all your projects.

Learn Selenium Python Training in Hyderabad

Read More:

Handling Dynamic Elements in Selenium

Automating Scrolling and Navigation

Validating Links and Images Using Selenium

Automating Captcha: What You Can and Can't Do

Selenium with Python: Common Errors and Fixes

Visit our IHub Talent Training Institute

Get Direction

Comments

Popular posts from this blog

Tosca Installation and Environment Setup

Understanding Tosca TestCases and Modules

How Tosca Handles TestData Parameterization