0
0
Selenium Pythontesting~20 mins

Select class for dropdowns in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dropdown Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Selenium Python code snippet?
Consider the following Selenium Python code that selects an option from a dropdown menu. What will be printed after execution?
Selenium Python
from selenium.webdriver.support.ui import Select

class MockElement:
    def __init__(self):
        self.selected_option = None

    def find_element(self, by, value):
        return self

    def click(self):
        pass

    def get_attribute(self, attr):
        if attr == 'value':
            return self.selected_option

select_element = MockElement()
select = Select(select_element)

# Simulate selecting by visible text
select.select_by_visible_text = lambda text: setattr(select_element, 'selected_option', text)
select.select_by_visible_text('Option 2')

print(select_element.selected_option)
AAttributeError
BNone
COption 1
DOption 2
Attempts:
2 left
💡 Hint
Focus on what the lambda function does when called.
assertion
intermediate
1:30remaining
Which assertion correctly verifies the selected option text in a dropdown?
You have a Selenium Select object named 'select'. Which assertion correctly checks that the currently selected option's visible text is 'Apple'?
Aassert select.first_selected_option.text == 'Apple'
Bassert select.selected_option == 'Apple'
Cassert select.get_selected_option() == 'Apple'
Dassert select.options.text == 'Apple'
Attempts:
2 left
💡 Hint
Check the Selenium Select API for the property that returns the selected option element.
locator
advanced
2:00remaining
Which locator strategy is best for finding a dropdown by its label text?
You want to locate a dropdown menu associated with the label text 'Country'. Which locator strategy is the most reliable and accessible?
Adriver.find_element('id', 'country')
Bdriver.find_element('css selector', 'select#country')
Cdriver.find_element('xpath', "//label[text()='Country']/following-sibling::select")
Ddriver.find_element('xpath', "//select[@name='country']")
Attempts:
2 left
💡 Hint
Think about how labels and inputs are connected for accessibility.
🔧 Debug
advanced
2:30remaining
Why does this Selenium code fail to select an option?
Given this code snippet, why does the dropdown selection fail?
Selenium Python
from selenium.webdriver.support.ui import Select

select_element = driver.find_element('id', 'fruits')
select = Select(select_element)
select.select_by_value('banana')

# The dropdown does not change selection after this code runs.
AThe element found is not a <select> element but a div styled as dropdown.
BThe Select class cannot select by value, only by visible text.
CThe option with value 'banana' does not exist in the dropdown.
DThe driver.find_element method is deprecated and returns None.
Attempts:
2 left
💡 Hint
Check the HTML tag of the element found by id 'fruits'.