Bird
0
0

What will be the output of the following Selenium Java code snippet?

medium📝 Predict Output Q13 of 15
Selenium Java - Handling Windows, Frames, and Alerts
What will be the output of the following Selenium Java code snippet?
driver.get("https://example.com");
String originalHandle = driver.getWindowHandle();
driver.switchTo().newWindow(WindowType.TAB);
driver.get("https://selenium.dev");
System.out.println(driver.getCurrentUrl());
driver.switchTo().window(originalHandle);
System.out.println(driver.getCurrentUrl());
Ahttps://selenium.dev https://selenium.dev
Bhttps://example.com https://selenium.dev
Chttps://selenium.dev https://example.com
Dhttps://example.com https://example.com
Step-by-Step Solution
Solution:
  1. Step 1: Understand window handles and tab switching

    The code opens example.com, saves the original window handle, then opens a new tab and navigates to selenium.dev.
  2. Step 2: Analyze the printed URLs

    First print shows the URL of the new tab (selenium.dev). Then it switches back to the original window and prints its URL (example.com).
  3. Final Answer:

    https://selenium.dev https://example.com -> Option C
  4. Quick Check:

    Switch tabs = different URLs printed [OK]
Quick Trick: Switching tabs changes current URL output [OK]
Common Mistakes:
MISTAKES
  • Assuming URLs remain the same after switching tabs
  • Not saving original window handle before opening new tab
  • Confusing getWindowHandle() with getWindowHandles()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes