0
0
Selenium Pythontesting~5 mins

Why POM organizes test code in Selenium Python

Choose your learning style9 modes available
Introduction

Page Object Model (POM) helps keep test code neat and easy to understand by separating page details from test steps.

When you want to test a website with many pages.
When you want to avoid repeating code for the same page elements.
When you want to make tests easier to update if the website changes.
When working in a team to keep code organized and clear.
When you want to write tests faster by reusing page actions.
Syntax
Selenium Python
from selenium.webdriver.common.by import By

class PageName:
    def __init__(self, driver):
        self.driver = driver
        self.element_locator = (By.ID, "example_id")

    def action_on_element(self):
        self.driver.find_element(*self.element_locator).click()

Each page is a class with locators and actions.

Tests use these classes instead of raw Selenium commands.

Examples
This class represents the login page with methods to interact with it.
Selenium Python
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()
The test uses the LoginPage class to perform login steps clearly.
Selenium Python
def test_login(driver):
    login_page = LoginPage(driver)
    login_page.enter_username("user1")
    login_page.enter_password("pass123")
    login_page.click_login()
Sample Program

This test uses POM to separate page details from test steps. The HomePage class holds locators and actions. The test calls these actions to perform a search.

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By

class HomePage:
    def __init__(self, driver):
        self.driver = driver
        self.search_box = (By.NAME, "q")
        self.search_button = (By.NAME, "btnK")

    def enter_search(self, text):
        self.driver.find_element(*self.search_box).send_keys(text)

    def click_search(self):
        self.driver.find_element(*self.search_button).click()

# Test script

def test_google_search():
    driver = webdriver.Chrome()
    driver.get("https://www.google.com")

    home = HomePage(driver)
    home.enter_search("Selenium POM")
    # Normally we would wait here, but for simplicity we just click
    home.click_search()

    print("Search performed successfully")
    driver.quit()

if __name__ == "__main__":
    test_google_search()
OutputSuccess
Important Notes

Using POM makes tests easier to read and maintain.

If page elements change, update only the page class, not all tests.

Keep locators private inside page classes to avoid duplication.

Summary

POM organizes test code by separating page details from test logic.

This makes tests cleaner, easier to update, and reusable.

It helps teams work together smoothly on test automation.