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
Practice
Solution
Step 1: Understand what action methods do
Action methods are designed to perform user-like actions such as clicking buttons or typing text on web elements.Step 2: Identify the role of action methods in page classes
They help keep test code clean by encapsulating these actions inside the page class, making tests easier to read and reuse.Final Answer:
To perform tasks like clicking or typing on web page elements -> Option AQuick Check:
Action methods = perform tasks on elements [OK]
- Confusing action methods with data storage
- Thinking action methods generate reports
- Believing action methods configure browsers
Solution
Step 1: Check method signature and usage of self
Inside a class, methods must have self as the first parameter to access instance variables like driver.Step 2: Verify correct locator usage
Using By.ID with the driver.find_element method is the correct way to locate elements in Selenium Python.Final Answer:
def click_button(self): self.driver.find_element(By.ID, 'btn').click() -> Option DQuick Check:
Method with self and correct locator = def click_button(self): self.driver.find_element(By.ID, 'btn').click() [OK]
- Omitting self parameter in method
- Using find_element without driver or self
- Passing locator as string without By.ID
def click_submit(self):
self.driver.find_element(By.ID, 'submit').click()
return 'Clicked'Solution
Step 1: Understand what happens when element is missing
If the element with ID 'submit' is not found, Selenium raises NoSuchElementException immediately.Step 2: Check method return behavior
The return 'Clicked' only happens after the click succeeds, so it won't be reached if exception occurs.Final Answer:
selenium.common.exceptions.NoSuchElementException will be raised -> Option BQuick Check:
Missing element causes NoSuchElementException [OK]
- Assuming method returns 'Clicked' even if element missing
- Thinking test passes silently without error
- Confusing exception with printed output
def enter_text(self, text):
input_box = self.driver.find_element(By.NAME, 'username')
input_box.sendkeys(text)Solution
Step 1: Check method call spelling
The correct Selenium method to send text is 'send_keys', not 'sendkeys'. This typo causes an AttributeError.Step 2: Verify other parts of the method
The method has self parameter and uses By.NAME correctly, so no changes needed there.Final Answer:
Change 'sendkeys' to 'send_keys' to fix the method call -> Option AQuick Check:
Correct method name is send_keys [OK]
- Misspelling send_keys method
- Changing locator type unnecessarily
- Removing self parameter incorrectly
Solution
Step 1: Understand how to handle dropdowns in Selenium Python
Selenium provides a Select class to handle dropdowns, which must be initialized with the dropdown element.Step 2: Check each option for correct usage
def select_option(self, text): dropdown = Select(self.driver.find_element(By.ID, 'menu')) dropdown.select_by_visible_text(text) correctly creates a Select object and calls select_by_visible_text. def select_option(self, text): dropdown = self.driver.find_element(By.ID, 'menu') dropdown.select_by_visible_text(text) tries to call select_by_visible_text directly on a WebElement, which is invalid. def select_option(self, text): self.driver.find_element(By.ID, 'menu').click() self.driver.find_element(By.XPATH, f'//option[text()="{text}"]').click() manually clicks options but is less clean. def select_option(self, text): self.driver.find_element(By.ID, 'menu').send_keys(text) uses send_keys which may not work reliably.Final Answer:
def select_option(self, text): dropdown = Select(self.driver.find_element(By.ID, 'menu')) dropdown.select_by_visible_text(text) -> Option CQuick Check:
Use Select class for dropdowns [OK]
- Calling select_by_visible_text on WebElement directly
- Not importing or using Select class
- Using send_keys instead of Select for dropdowns
Ready to prove it?
Pass the test and this pattern moves unknown
