Bird
0
0

Given the following code snippet, what will be the selected option's visible text after execution?

medium📝 Predict Output Q13 of 15
Selenium Java - Handling Form Elements
Given the following code snippet, what will be the selected option's visible text after execution?
WebElement dropdown = driver.findElement(By.id("colors"));
Select select = new Select(dropdown);
select.selectByIndex(2);
String selected = select.getFirstSelectedOption().getText();

Assuming the dropdown options are:
0: Red
1: Green
2: Blue
3: Yellow
A"Blue"
B"Red"
C"Yellow"
D"Green"
Step-by-Step Solution
Solution:
  1. Step 1: Understand selectByIndex behavior

    selectByIndex(2) selects the option at index 2, counting from 0.
  2. Step 2: Map index to option text

    Index 0 = Red, 1 = Green, 2 = Blue, so the selected option is "Blue".
  3. Final Answer:

    "Blue" -> Option A
  4. Quick Check:

    Index 2 = Blue [OK]
Quick Trick: Index starts at 0, so 2 means third option [OK]
Common Mistakes:
MISTAKES
  • Assuming index starts at 1
  • Confusing selectByIndex with selectByValue
  • Misreading option order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes