Bird
0
0

How can you select the last option in a dropdown without knowing the number of options beforehand using Selenium Java?

hard📝 Application Q9 of 15
Selenium Java - Handling Form Elements
How can you select the last option in a dropdown without knowing the number of options beforehand using Selenium Java?
AUse select.selectByIndex(-1);
BUse select.selectByIndex(select.getOptions().size() - 1);
CUse select.selectByVisibleText("last option");
DUse select.selectByValue("last");
Step-by-Step Solution
Solution:
  1. Step 1: Get total options count

    Use select.getOptions().size() to find how many options exist.
  2. Step 2: Select last option by index

    Index of last option is total size minus 1, so use selectByIndex(size - 1).
  3. Final Answer:

    Use select.selectByIndex(select.getOptions().size() - 1); -> Option B
  4. Quick Check:

    Select last option by index size-1 [OK]
Quick Trick: Last index = total options count minus one [OK]
Common Mistakes:
MISTAKES
  • Using negative index
  • Assuming value or text "last"
  • Hardcoding index without size check

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes