How to Create Page Class in Selenium Python | Page Object Model
To create a page class in Selenium Python, define a Python class representing a web page with locators as class variables and methods for interactions. Use
__init__ to pass the WebDriver instance and write methods to perform actions or get data from the page elements.Syntax
A page class in Selenium Python typically includes:
- Class definition: Represents a web page.
- Locators: Store element selectors as variables.
- Constructor (
__init__): Accepts the WebDriver instance. - Methods: Define actions like clicking buttons or entering text.
python
class PageClass: def __init__(self, driver): self.driver = driver self.element_locator = "locator_value" def action_method(self): element = self.driver.find_element("by", self.element_locator) element.click()
Example
This example shows a simple page class for a login page with username, password fields, and a login button. It includes methods to enter credentials and click login.
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() # Usage example (requires a running WebDriver and a test page): # driver = webdriver.Chrome() # driver.get("https://example.com/login") # login_page = LoginPage(driver) # login_page.enter_username("user1") # login_page.enter_password("pass123") # login_page.click_login()
Common Pitfalls
- Hardcoding locators inside methods: Makes maintenance hard; use class variables instead.
- Not passing the WebDriver instance: The page class needs the driver to find elements.
- Using find_element repeatedly without storing locators: Leads to duplicated code.
- Mixing test logic with page class: Keep page classes only for page interactions.
python
from selenium.webdriver.common.by import By class WrongPage: def click_button(self): # Bad: locator hardcoded inside method self.driver.find_element(By.ID, "submitBtn").click() class RightPage: submit_button = (By.ID, "submitBtn") def __init__(self, driver): self.driver = driver def click_button(self): self.driver.find_element(*self.submit_button).click()
Quick Reference
Use this quick reference to remember key points when creating page classes:
- Define locators as tuples with
Bystrategies. - Pass WebDriver instance via
__init__. - Write clear methods for each user action.
- Keep page classes free of assertions or test logic.
- Use
*operator to unpack locator tuples infind_element.
Key Takeaways
Create a page class by defining locators and methods representing page elements and actions.
Always pass the WebDriver instance to the page class constructor to interact with the browser.
Store locators as class variables using Selenium's By strategies for easy maintenance.
Keep test logic separate from page classes to maintain clean code structure.
Use methods in the page class to perform user actions like clicking or typing.