A page class helps organize web page elements and actions in one place. It makes tests easier to read and maintain.
0
0
Page class structure in Selenium Python
Introduction
When testing a website with many pages and elements.
When you want to reuse code for interacting with the same page.
When you want to keep test scripts clean and simple.
When multiple testers work on the same project and need clear structure.
Syntax
Selenium Python
class PageName: def __init__(self, driver): self.driver = driver # Define locators here def action_method(self): # Code to perform action on page element pass
The __init__ method sets up the web driver for the page.
Locators and actions are grouped inside the class for clarity.
Examples
This class stores locators and methods to interact with the login page.
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()
This class handles the home page with a search box and a search action.
Selenium Python
from selenium.webdriver.common.by import By class HomePage: def __init__(self, driver): self.driver = driver self.search_box = (By.NAME, "search") def search(self, text): self.driver.find_element(*self.search_box).send_keys(text) self.driver.find_element(*self.search_box).submit()
Sample Program
This script opens a browser, goes to the login page, enters username and password, clicks login, and prints a success message.
Selenium Python
from selenium import webdriver 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() # Test script driver = webdriver.Chrome() driver.get("https://example.com/login") login_page = LoginPage(driver) login_page.enter_username("testuser") login_page.enter_password("password123") login_page.click_login() print("Login actions performed successfully.") driver.quit()
OutputSuccess
Important Notes
Use tuples like (By.ID, "value") for locators to follow Selenium best practices.
Keep locators private if you want to hide them from outside the class by prefixing with underscore (e.g., _username_input).
Page classes help reduce repeated code and make tests easier to update when the page changes.
Summary
Page classes group web elements and actions for one page.
This structure makes tests cleaner and easier to maintain.
Use locators and methods inside the class to interact with the page.