Bird
0
0

Given String originalHandle = driver.getWindowHandle(); and Set<String> allHandles = driver.getWindowHandles();, which code correctly switches to the new window?

hard📝 Application Q15 of 15
Selenium Java - Handling Windows, Frames, and Alerts
You want to switch to a newly opened window different from the original one. Given String originalHandle = driver.getWindowHandle(); and Set<String> allHandles = driver.getWindowHandles();, which code correctly switches to the new window?
Afor (String handle : allHandles) { if (!handle.equals(originalHandle)) { driver.switchTo().window(handle); break; } }
Bdriver.switchTo().window(allHandles.iterator().next());
Cdriver.switchTo().window(originalHandle);
Ddriver.switchTo().window(allHandles.toArray()[0].toString());
Step-by-Step Solution
Solution:
  1. Step 1: Identify the original window handle

    We have the original window handle stored in originalHandle.
  2. Step 2: Loop through all handles to find the new one

    Iterate over all handles and switch to the one that is not equal to the original handle.
  3. Step 3: Switch to the new window

    Once found, call driver.switchTo().window(handle) and break the loop.
  4. Final Answer:

    for (String handle : allHandles) { if (!handle.equals(originalHandle)) { driver.switchTo().window(handle); break; } } -> Option A
  5. Quick Check:

    Switch to handle != originalHandle [OK]
Quick Trick: Loop handles, skip original, switch to new [OK]
Common Mistakes:
  • Switching to first handle without checking if it's original
  • Using toArray()[0] which may be original window
  • Switching to originalHandle instead of new window

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes