Bird
0
0

What is wrong with this code?

medium📝 Debug Q7 of 15
Selenium Java - Handling Form Elements
What is wrong with this code?
WebElement dropdown = driver.findElement(By.id("menu"));
Select select = new Select(dropdown);
select.selectByIndex(5);

Assuming the dropdown has only 4 options (index 0 to 3).
AselectByIndex(5) will select the first option
BselectByIndex(5) will select the last option
CNo error, it will select index 5
DselectByIndex(5) will throw an exception
Step-by-Step Solution
Solution:
  1. Step 1: Understand index bounds

    Index 5 is out of range for 4 options (0 to 3).
  2. Step 2: Behavior of selectByIndex with invalid index

    Calling selectByIndex with invalid index throws NoSuchElementException.
  3. Final Answer:

    selectByIndex(5) will throw an exception -> Option D
  4. Quick Check:

    Invalid index causes exception [OK]
Quick Trick: Index must be within options range [OK]
Common Mistakes:
MISTAKES
  • Assuming it selects last or first option silently
  • Ignoring exception possibility
  • Not checking dropdown size before selecting

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes