Bird
Raised Fist0
Selenium Pythontesting~20 mins

Action methods in page class in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Action Method Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of action method performing click
What will be the output of the following Selenium Python code snippet when the click_login method is called?
Selenium Python
from selenium.webdriver.common.by import By

class LoginPage:
    def __init__(self, driver):
        self.driver = driver
        self.login_button = (By.ID, 'loginBtn')

    def click_login(self):
        element = self.driver.find_element(*self.login_button)
        element.click()
        return 'Clicked'

# Assume driver is a valid WebDriver instance and login button is present.
A'Clicked'
BNo output, method returns None
CRaises NoSuchElementException
DReturns the WebElement object
Attempts:
2 left
💡 Hint
Look at what the method returns after clicking the element.
❓ assertion
intermediate
2:00remaining
Correct assertion for verifying text after action
Given a page class with a method get_welcome_message that returns the welcome text after login, which assertion correctly verifies the message is 'Welcome User'?
Selenium Python
from selenium.webdriver.common.by import By

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

    def get_welcome_message(self):
        return self.driver.find_element(By.ID, 'welcomeMsg').text

# After login, test calls home_page.get_welcome_message()
Aassert home_page.get_welcome_message() is None
Bassert 'Welcome User' in home_page.get_welcome_message()
Cassert home_page.get_welcome_message() == 'Welcome User'
Dassert home_page.get_welcome_message() != 'Welcome User'
Attempts:
2 left
💡 Hint
Check for exact equality of the returned text.
❓ locator
advanced
2:00remaining
Best locator strategy for action method
Which locator is the best choice for an action method that clicks a submit button with a unique class name 'submit-btn'?
ABy.ID, 'submit-btn'
BBy.CLASS_NAME, 'submit-btn'
CBy.XPATH, "//button[@class='submit-btn']"
DBy.CSS_SELECTOR, 'button.submit-btn'
Attempts:
2 left
💡 Hint
Consider specificity and reliability of locators.
🔧 Debug
advanced
2:00remaining
Identify error in action method
What error will occur when running this action method that tries to send keys to a text field?
Selenium Python
from selenium.webdriver.common.by import By

class SearchPage:
    def __init__(self, driver):
        self.driver = driver
        self.search_input = (By.ID, 'searchBox')

    def enter_search_term(self, term):
        element = self.driver.find_element(*self.search_input)
        element.send_keys(term)
ANoSuchElementException at runtime
BTypeError: find_element() missing 1 required positional argument
CSyntaxError due to missing colon
DAttributeError: 'tuple' object has no attribute 'send_keys'
Attempts:
2 left
💡 Hint
Check how find_element is called with locator tuple.
❓ framework
expert
3:00remaining
Design pattern for reusable action methods
In a Selenium test framework using page classes, which design approach best supports reusable action methods for different pages?
ACreate a BasePage class with common action methods inherited by all page classes
BWrite all action methods directly in test scripts without page classes
CUse global functions outside classes for all actions
DDuplicate action methods in each page class separately
Attempts:
2 left
💡 Hint
Think about code reuse and maintainability.

Practice

(1/5)
1. What is the main purpose of action methods in a page class in Selenium Python?
easy
A. To perform tasks like clicking or typing on web page elements
B. To store test data separately from test scripts
C. To generate reports after test execution
D. To configure browser settings before tests

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    To perform tasks like clicking or typing on web page elements -> Option A
  4. Quick Check:

    Action methods = perform tasks on elements [OK]
Hint: Action methods do things on page elements, like clicks or typing [OK]
Common Mistakes:
  • Confusing action methods with data storage
  • Thinking action methods generate reports
  • Believing action methods configure browsers
2. Which of the following is the correct way to define an action method that clicks a button using Selenium Python inside a page class?
easy
A. def click_button(): driver.find_element(By.ID, 'btn').click()
B. def click_button(self): self.driver.find_element('btn').click()
C. def click_button(self): find_element(By.ID, 'btn').click()
D. def click_button(self): self.driver.find_element(By.ID, 'btn').click()

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    def click_button(self): self.driver.find_element(By.ID, 'btn').click() -> Option D
  4. Quick Check:

    Method with self and correct locator = def click_button(self): self.driver.find_element(By.ID, 'btn').click() [OK]
Hint: Always include self and use By.ID for locators [OK]
Common Mistakes:
  • Omitting self parameter in method
  • Using find_element without driver or self
  • Passing locator as string without By.ID
3. Given the following page class method, what will be the output if the button with ID 'submit' is not present on the page?
def click_submit(self):
    self.driver.find_element(By.ID, 'submit').click()
    return 'Clicked'
medium
A. 'Clicked' will be returned
B. selenium.common.exceptions.NoSuchElementException will be raised
C. No output, test will silently pass
D. 'submit' string will be printed

Solution

  1. Step 1: Understand what happens when element is missing

    If the element with ID 'submit' is not found, Selenium raises NoSuchElementException immediately.
  2. Step 2: Check method return behavior

    The return 'Clicked' only happens after the click succeeds, so it won't be reached if exception occurs.
  3. Final Answer:

    selenium.common.exceptions.NoSuchElementException will be raised -> Option B
  4. Quick Check:

    Missing element causes NoSuchElementException [OK]
Hint: Missing element causes exception, no return happens [OK]
Common Mistakes:
  • Assuming method returns 'Clicked' even if element missing
  • Thinking test passes silently without error
  • Confusing exception with printed output
4. Identify the error in this action method and choose the correct fix:
def enter_text(self, text):
    input_box = self.driver.find_element(By.NAME, 'username')
    input_box.sendkeys(text)
medium
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

  1. Step 1: Check method call spelling

    The correct Selenium method to send text is 'send_keys', not 'sendkeys'. This typo causes an AttributeError.
  2. Step 2: Verify other parts of the method

    The method has self parameter and uses By.NAME correctly, so no changes needed there.
  3. Final Answer:

    Change 'sendkeys' to 'send_keys' to fix the method call -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    def select_option(self, text): dropdown = Select(self.driver.find_element(By.ID, 'menu')) dropdown.select_by_visible_text(text) -> Option C
  4. Quick Check:

    Use Select class for dropdowns [OK]
Hint: Use Select class to pick dropdown options by visible text [OK]
Common Mistakes:
  • Calling select_by_visible_text on WebElement directly
  • Not importing or using Select class
  • Using send_keys instead of Select for dropdowns
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard