Selenium + Python · Lesson 34 of 43

0/43 done

Module 34: Page Object Model

34. Scalable Page Object Model Architecture in Python

18 minlesson

Your task

Implement simple LoginPage class:

  1. __init__(self, driver) storing self.driver = driver.
  2. USERNAME = (By.ID, "user").
  3. enter_username(self, text) calling self.driver.find_element(*self.USERNAME).send_keys(text).

Write it in the editor, then press Check answer (Ctrl + Enter).

Enterprise Page Object Pattern in Python

python
from selenium.webdriver.common.by import By

class BasePage:
    def __init__(self, driver):
        self.driver = driver

    def open(self, url: str):
        self.driver.get(url)

class LoginPage(BasePage):
    USERNAME_INPUT = (By.ID, "username")
    PASSWORD_INPUT = (By.ID, "password")
    LOGIN_BTN = (By.ID, "login-btn")

    def login(self, username, password):
        self.driver.find_element(*self.USERNAME_INPUT).send_keys(username)
        self.driver.find_element(*self.PASSWORD_INPUT).send_keys(password)
        self.driver.find_element(*self.LOGIN_BTN).click()

Key Takeaways & Walkthrough Summary

Welcome to Phase 5! The Page Object Model is the gold standard design pattern for maintainable automation. In this lesson, you will architect a BasePage class, encapsulate locators as tuples, and separate actions from assertions. Let's design scalable page objects!

Part of the Selenium + Python course (Selenium with Python). Practice this concept live in the Selenium Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

What does *self.USERNAME do?

The asterisk unpacks the tuple (By.ID, 'user') into two separate arguments for find_element(By.ID, 'user').

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Python
Skip