Selenium + Python · Lesson 34 of 43
Module 34: Page Object Model
Your task
Implement simple LoginPage class:
Write it in the editor, then press Check answer (Ctrl + Enter).
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()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.
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.
___