0
0
Selenium Pythontesting~20 mins

Element locators in page class in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Locator Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
locator
intermediate
2:00remaining
Identify the best locator for a login button

You have a login button with HTML: <button id='loginBtn' class='btn primary'>Login</button>. Which locator is the most reliable and efficient to use in a Selenium page class?

Adriver.find_element(By.XPATH, "//button[text()='Login']")
Bdriver.find_element(By.CLASS_NAME, 'btn')
Cdriver.find_element(By.ID, 'loginBtn')
Ddriver.find_element(By.CSS_SELECTOR, 'button.primary')
Attempts:
2 left
💡 Hint

Think about uniqueness and speed of locating elements.

assertion
intermediate
2:00remaining
Choose the correct assertion for verifying page title

You want to check if the page title is exactly 'Welcome Page' after login. Which assertion is correct in a Python unittest test case?

Aself.assertEqual(driver.title, 'Welcome Page')
Bself.assertTrue(driver.title == 'Welcome Page')
Cself.assertIn('Welcome', driver.title)
Dself.assertIs(driver.title, 'Welcome Page')
Attempts:
2 left
💡 Hint

Check which assertion method compares values for equality properly.

Predict Output
advanced
2:00remaining
What is the output of this Selenium locator code?

Given the following page class snippet, what will print(len(page.buttons)) output if the page has 3 buttons with class 'btn'?

Selenium Python
from selenium.webdriver.common.by import By

class Page:
    def __init__(self, driver):
        self.driver = driver
    
    @property
    def buttons(self):
        return self.driver.find_elements(By.CLASS_NAME, 'btn')

# Assume driver is a Selenium WebDriver instance with 3 buttons having class 'btn'
page = Page(driver)
print(len(page.buttons))
ARaises NoSuchElementException
B1
C0
D3
Attempts:
2 left
💡 Hint

Remember that find_elements returns a list of matching elements.

🔧 Debug
advanced
2:00remaining
Find the error in this page class locator

What is wrong with this locator in the page class?

Selenium Python
from selenium.webdriver.common.by import By

class LoginPage:
    def __init__(self, driver):
        self.driver = driver
    
    username_input = (By.ID, 'username')
    
    def enter_username(self, name):
        self.driver.find_element(*self.username_input).send_keys(name)
ANo error, code works fine
Bfind_element expects two arguments, not a tuple
Csend_keys is called on a tuple, not a WebElement
Dusername_input should be a method, not a tuple
Attempts:
2 left
💡 Hint

Check how find_element is called with locators.

framework
expert
2:00remaining
Which test framework feature best supports page class locators?

In a Selenium test suite using Python, which feature of the pytest framework helps manage page class locators efficiently and promotes clean test code?

AUsing pytest fixtures to initialize page objects
BUsing pytest.mark.parametrize to test multiple locators
CUsing pytest.raises to catch locator errors
DUsing pytest.skip to ignore locator tests
Attempts:
2 left
💡 Hint

Think about how to reuse page objects across tests.