Bird
0
0

You want to open a new tab, navigate to a URL, then switch back to the original tab and verify its title. Which code snippet correctly does this in Selenium Java?

hard📝 Application Q15 of 15
Selenium Java - Handling Windows, Frames, and Alerts
You want to open a new tab, navigate to a URL, then switch back to the original tab and verify its title. Which code snippet correctly does this in Selenium Java?
AString original = driver.getWindowHandle(); driver.switchTo().newWindow(WindowType.TAB); driver.get("https://selenium.dev"); driver.switchTo().window(original); assert driver.getTitle().contains("Example");
Bdriver.switchTo().newWindow(WindowType.TAB); driver.get("https://selenium.dev"); String original = driver.getWindowHandle(); driver.switchTo().window(original); assert driver.getTitle().contains("Example");
CString original = driver.getWindowHandle(); driver.newWindow(WindowType.TAB); driver.get("https://selenium.dev"); driver.switchTo().window(original); assert driver.getTitle().contains("Example");
Ddriver.get("https://selenium.dev"); driver.switchTo().newWindow(WindowType.TAB); driver.switchTo().window(original); assert driver.getTitle().contains("Example");
Step-by-Step Solution
Solution:
  1. Step 1: Save original window handle before opening new tab

    We must save the original window handle before opening a new tab to switch back later.
  2. Step 2: Open new tab, navigate, then switch back and assert title

    String original = driver.getWindowHandle(); driver.switchTo().newWindow(WindowType.TAB); driver.get("https://selenium.dev"); driver.switchTo().window(original); assert driver.getTitle().contains("Example"); correctly calls switchTo().newWindow(WindowType.TAB), navigates, switches back using saved handle, and asserts title.
  3. Final Answer:

    Option A code snippet correctly performs all steps -> Option A
  4. Quick Check:

    Save handle, open tab, switch back, assert [OK]
Quick Trick: Save handle before new tab, switch back to assert [OK]
Common Mistakes:
  • Saving window handle after opening new tab
  • Calling newWindow() without switchTo()
  • Not switching back before assertion

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes