Action methods in page class in Selenium Python - Build an Automation Script
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC 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: str): WebDriverWait(self.driver, 10).until( EC.visibility_of_element_located(self.username_input) ).clear() self.driver.find_element(*self.username_input).send_keys(username) def enter_password(self, password: str): WebDriverWait(self.driver, 10).until( EC.visibility_of_element_located(self.password_input) ).clear() self.driver.find_element(*self.password_input).send_keys(password) def click_login(self): WebDriverWait(self.driver, 10).until( EC.element_to_be_clickable(self.login_button) ).click() def test_login(): driver = webdriver.Chrome() driver.get('https://example.com/login') login_page = LoginPage(driver) login_page.enter_username('testuser') login_page.enter_password('Test@1234') login_page.click_login() WebDriverWait(driver, 10).until( EC.url_contains('/dashboard') ) assert '/dashboard' in driver.current_url, 'Dashboard URL not found after login' driver.quit()
This script uses Selenium with Python to automate the login process.
The LoginPage class is a page object that holds locators and action methods for the login page. Each method waits explicitly for the element to be ready before interacting with it. This avoids timing issues.
The test_login function opens the browser, navigates to the login page, and uses the page object's methods to enter username and password, then click login.
After clicking login, it waits until the URL contains '/dashboard' to confirm successful login, then asserts this condition.
Finally, the browser is closed with driver.quit().
This structure keeps test steps clear and separates page details from test logic, following best practices.
Now add data-driven testing with 3 different username and password combinations