0
0
Selenium Pythontesting~20 mins

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

Choose your learning style9 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.