Bird
0
0

Given the following code snippet, what will be the output?

medium📝 Predict Output Q4 of 15
Selenium Java - Handling Windows, Frames, and Alerts
Given the following code snippet, what will be the output?
String mainWindow = driver.getWindowHandle();
Set allWindows = driver.getWindowHandles();
for(String window : allWindows) {
  if(!window.equals(mainWindow)) {
    driver.switchTo().window(window);
    System.out.println(driver.getTitle());
  }
}
AThrows an exception because of wrong window handle
BPrints the title of the main window only
CPrints nothing because the loop is empty
DPrints the title of all windows except the main window
Step-by-Step Solution
Solution:
  1. Step 1: Understand the loop logic

    The code loops through all window handles and switches to each window except the main one.
  2. Step 2: Analyze the output

    For each switched window, it prints the window's title. So it prints titles of all windows except the main window.
  3. Final Answer:

    Prints the title of all windows except the main window -> Option D
  4. Quick Check:

    Loop excludes main window, prints others [OK]
Quick Trick: Exclude main window handle to switch others [OK]
Common Mistakes:
  • Assuming it prints main window title
  • Thinking it throws exception for valid handles
  • Believing the loop is empty or skipped

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes