Given a dropdown with options having values '1', '2', '3', what will be the selected option text after selecting by value '2'?
from selenium.webdriver.support.ui import Select class DummySelect: def __init__(self): self.options = [{'value': '1', 'text': 'One'}, {'value': '2', 'text': 'Two'}, {'value': '3', 'text': 'Three'}] self.selected_option = None def select_by_value(self, value): for option in self.options: if option['value'] == value: self.selected_option = option['text'] break else: self.selected_option = None select = DummySelect() select.select_by_value('2') result = select.selected_option
Remember, selecting by value matches the option's value attribute, not its text.
Selecting by value '2' matches the option with value '2', which has text 'Two'. So the selected option text is 'Two'.
Given a dropdown with options 'Apple', 'Banana', 'Cherry', what will be the selected option value after selecting by visible text 'Banana'?
from selenium.webdriver.support.ui import Select class DummySelect: def __init__(self): self.options = [{'value': 'a', 'text': 'Apple'}, {'value': 'b', 'text': 'Banana'}, {'value': 'c', 'text': 'Cherry'}] self.selected_value = None def select_by_visible_text(self, text): for option in self.options: if option['text'] == text: self.selected_value = option['value'] break else: self.selected_value = None select = DummySelect() select.select_by_visible_text('Banana') result = select.selected_value
Selecting by visible text matches the option's text, but the result asked is the value attribute.
Selecting by visible text 'Banana' finds the option with text 'Banana' which has value 'b'. So the selected value is 'b'.
Given a dropdown with options ['Red', 'Green', 'Blue'], what is the selected option text after selecting by index 1?
from selenium.webdriver.support.ui import Select class DummySelect: def __init__(self): self.options = [{'value': 'r', 'text': 'Red'}, {'value': 'g', 'text': 'Green'}, {'value': 'b', 'text': 'Blue'}] self.selected_option = None def select_by_index(self, index): if 0 <= index < len(self.options): self.selected_option = self.options[index]['text'] else: self.selected_option = None select = DummySelect() select.select_by_index(1) result = select.selected_option
Indexing starts at 0, so index 1 is the second option.
Index 1 corresponds to the second option, which has text 'Green'. So the selected option text is 'Green'.
Identify the code snippet that will cause a syntax error when trying to select an option by value.
from selenium.webdriver.support.ui import Select select = Select(driver.find_element_by_id('dropdown'))
Look for missing quotes or parentheses.
Option B has a missing closing quote and parenthesis causing SyntaxError. Option C has an extra closing parenthesis causing SyntaxError. Option D lacks quotes causing NameError, not SyntaxError. Option A is correct syntax.
Given a dropdown with 4 options, what error occurs when selecting by index 5 and why?
from selenium.webdriver.support.ui import Select class DummySelect: def __init__(self): self.options = [{'value': '1', 'text': 'One'}, {'value': '2', 'text': 'Two'}, {'value': '3', 'text': 'Three'}, {'value': '4', 'text': 'Four'}] def select_by_index(self, index): return self.options[index]['text'] select = DummySelect() result = select.select_by_index(5)
Check the length of the options list and the index used.
The options list has 4 items indexed 0 to 3. Index 5 is out of range, so accessing options[5] raises IndexError.