Bird
0
0

Identify the error in the following code snippet:

medium📝 Debug Q6 of 15
Selenium Java - Handling Windows, Frames, and Alerts
Identify the error in the following code snippet:
Set handles = driver.getWindowHandles();
String firstHandle = handles.get(0);
ANo error; code works fine
BgetWindowHandles() returns a List, so get(0) is valid
CThe variable handles should be declared as List<String>
DSet does not have a get(int) method; use iterator instead
Step-by-Step Solution
Solution:
  1. Step 1: Check the data type of handles

    handles is a Set, which does not support get(int) method.
  2. Step 2: Correct way to access first element

    Use an iterator or convert Set to List to access by index.
  3. Final Answer:

    Set does not have a get(int) method; use iterator instead -> Option D
  4. Quick Check:

    Set has no get(index) method [OK]
Quick Trick: Use iterator to access Set elements, not get(index) [OK]
Common Mistakes:
MISTAKES
  • Trying to access Set elements by index
  • Assuming getWindowHandles() returns List
  • Ignoring type mismatch errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes