Action methods help you perform tasks on a web page like clicking buttons or typing text. They keep your test code clean and easy to understand.
When you want to click a button on a web page during a test.
When you need to enter text into a form field automatically.
When you want to select an option from a dropdown menu.
When you want to check if a page element is visible before interacting.
When you want to reuse common page actions in multiple tests.
Syntax
Selenium Python
from selenium.webdriver.common.by import By
class PageClass:
def __init__(self, driver):
self.driver = driver
def action_method(self):
element = self.driver.find_element(By.ID, 'element_id')
element.click()
Action methods are inside a page class that represents a web page.
They use locators to find elements and perform actions like click or send keys.
A. Change 'sendkeys' to 'send_keys' to fix the method call
B. Replace 'By.NAME' with 'By.ID' to locate the element correctly
C. Add 'self' parameter to the method definition
D. Use 'find_elements' instead of 'find_element' to get multiple elements
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 A
Quick Check:
Correct method name is send_keys [OK]
Hint: Selenium method is send_keys, not sendkeys [OK]
Common Mistakes:
Misspelling send_keys method
Changing locator type unnecessarily
Removing self parameter incorrectly
5. You want to create an action method in your page class that selects an option from a dropdown by visible text. Which of the following is the best implementation?
hard
A. def select_option(self, text):
self.driver.find_element(By.ID, 'menu').click()
self.driver.find_element(By.XPATH, f'//option[text()="{text}"]').click()
B. def select_option(self, text):
dropdown = self.driver.find_element(By.ID, 'menu')
dropdown.select_by_visible_text(text)
C. def select_option(self, text):
dropdown = Select(self.driver.find_element(By.ID, 'menu'))
dropdown.select_by_visible_text(text)
D. def select_option(self, text):
self.driver.find_element(By.ID, 'menu').send_keys(text)
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.