Bird
0
0

After clicking a link that opens a new window, which code snippet correctly switches to the new window using getWindowHandles()?

hard📝 Application Q8 of 15
Selenium Java - Handling Windows, Frames, and Alerts
After clicking a link that opens a new window, which code snippet correctly switches to the new window using getWindowHandles()?
AString original = driver.getWindowHandle(); Set<String> allHandles = driver.getWindowHandles(); for (String handle : allHandles) { if (!handle.equals(original)) { driver.switchTo().window(handle); break; } }
Bdriver.switchTo().window(driver.getWindowHandles().iterator().next());
Cdriver.switchTo().window(driver.getWindowHandle());
Ddriver.switchTo().window(null);
Step-by-Step Solution
Solution:
  1. Step 1: Store original window handle

    Save the current window handle before clicking the link.
  2. Step 2: Get all window handles

    Retrieve all open window handles after the new window opens.
  3. Step 3: Loop through handles and switch

    Switch to the handle that is not the original window.
  4. Final Answer:

    String original = driver.getWindowHandle(); Set allHandles = driver.getWindowHandles(); for (String handle : allHandles) { if (!handle.equals(original)) { driver.switchTo().window(handle); break; } } correctly implements this logic.
  5. Quick Check:

    Compare handles to find new window [OK]
Quick Trick: Switch to handle not equal to original [OK]
Common Mistakes:
MISTAKES
  • Switching to first handle without checking
  • Switching to original handle again
  • Passing null to switchTo().window()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes