Bird
Raised Fist0
Selenium Javatesting~20 mins

Switching between windows in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Window Switching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Selenium Java code snippet?
Consider the following Selenium Java code that opens a new window and switches to it. What will be the value of currentTitle after execution?
Selenium Java
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
String originalWindow = driver.getWindowHandle();
// Assume a new window is opened here
for (String windowHandle : driver.getWindowHandles()) {
    if (!windowHandle.equals(originalWindow)) {
        driver.switchTo().window(windowHandle);
        break;
    }
}
String currentTitle = driver.getTitle();
AThrows NoSuchWindowException
B"" (empty string)
Cnull
D"Example Domain"
Attempts:
2 left
💡 Hint
Remember that switching to the new window changes the driver's context to that window, and getTitle() returns the page title of the current window.
❓ assertion
intermediate
1:30remaining
Which assertion correctly verifies the switch to a new window?
You have switched to a new window in Selenium Java. Which assertion correctly checks that the driver is no longer on the original window?
Selenium Java
String originalWindow = driver.getWindowHandle();
// code to open and switch to new window
String currentWindow = driver.getWindowHandle();
AassertNotEquals(originalWindow, currentWindow);
BassertEquals(originalWindow, currentWindow);
CassertTrue(originalWindow.contains(currentWindow));
DassertNull(currentWindow);
Attempts:
2 left
💡 Hint
After switching, the current window handle should be different from the original.
❓ locator
advanced
1:30remaining
Which locator strategy is best to find the button that opens a new window?
You want to locate a button that opens a new browser window. The button has the text "Open Window" and a unique id "openWindowBtn". Which locator is best practice in Selenium Java?
ABy.className("openWindowBtn")
BBy.xpath("//button[text()='Open Window']")
CBy.id("openWindowBtn")
DBy.cssSelector("button#openWindowBtn")
Attempts:
2 left
💡 Hint
Using unique ids is the most reliable and fastest locator.
🔧 Debug
advanced
2:00remaining
Why does this Selenium Java code throw NoSuchWindowException?
Examine the code below. It throws NoSuchWindowException. What is the most likely cause?
Selenium Java
String originalWindow = driver.getWindowHandle();
driver.switchTo().window("nonexistentWindowHandle");
// further code
AThe driver has not been initialized properly.
BThe window handle "nonexistentWindowHandle" does not exist in the current session.
CThe browser does not support multiple windows.
DThe switchTo() method requires a WebElement, not a window handle.
Attempts:
2 left
💡 Hint
Check if the window handle you are switching to is valid and present.
❓ framework
expert
2:30remaining
In a Selenium Java test framework, how to reliably switch back to the original window after multiple window switches?
You have a test that opens multiple windows and switches between them. What is the best practice to switch back to the original window at the end of the test?
AStore the original window handle at the start and call driver.switchTo().window(originalWindowHandle) when needed.
BCall driver.close() on all windows except the current one to return to the original.
CUse driver.switchTo().defaultContent() to switch back to the original window.
DRestart the WebDriver session to reset to the original window.
Attempts:
2 left
💡 Hint
Remember that defaultContent() switches frames, not windows.

Practice

(1/5)
1. What does the getWindowHandle() method return in Selenium WebDriver?
easy
A. The unique ID of the current browser window or tab
B. A list of all open browser windows or tabs
C. The title of the current browser window
D. The URL of the current page

Solution

  1. Step 1: Understand getWindowHandle() purpose

    This method returns a unique string ID representing the current window or tab the driver is controlling.
  2. Step 2: Differentiate from getWindowHandles()

    Unlike getWindowHandles(), which returns all window IDs, getWindowHandle() returns only the current one.
  3. Final Answer:

    The unique ID of the current browser window or tab -> Option A
  4. Quick Check:

    getWindowHandle() = current window ID [OK]
Hint: Remember: singular handle = current window only [OK]
Common Mistakes:
  • Confusing getWindowHandle() with getWindowHandles()
  • Thinking it returns window title or URL
  • Assuming it returns multiple windows
2. Which of the following is the correct syntax to switch to a new window using its handle in Selenium Java?
easy
A. driver.switchToWindowHandle(windowHandle);
B. driver.switchToWindow(windowHandle);
C. driver.switchTo().window(windowHandle);
D. driver.switch.window(windowHandle);

Solution

  1. Step 1: Recall correct switch syntax

    The correct method chain is driver.switchTo().window(handle) to switch focus to a window by its handle.
  2. Step 2: Identify incorrect options

    Options A, B, and C use invalid method names or chaining not supported by Selenium WebDriver API.
  3. Final Answer:

    driver.switchTo().window(windowHandle); -> Option C
  4. Quick Check:

    Use switchTo().window() to switch windows [OK]
Hint: Use driver.switchTo().window(handle) always [OK]
Common Mistakes:
  • Using incorrect method names like switchToWindow()
  • Missing the switchTo() part before window()
  • Trying to call window() directly on driver
3. Consider the following code snippet:
String mainWindow = driver.getWindowHandle();
Set<String> allWindows = driver.getWindowHandles();
for (String window : allWindows) {
    if (!window.equals(mainWindow)) {
        driver.switchTo().window(window);
        driver.close();
    }
}
driver.switchTo().window(mainWindow);
System.out.println(driver.getTitle());
What will this code do?
medium
A. Close all windows including the main one and throw an error
B. Close all windows except the main one and print the main window's title
C. Close only the main window and print the last window's title
D. Switch to the last window and print its title without closing any window

Solution

  1. Step 1: Analyze window handles and loop

    The code saves the main window handle, loops through all windows, and closes any window not equal to mainWindow.
  2. Step 2: Switch back and print title

    After closing other windows, it switches back to mainWindow and prints its title.
  3. Final Answer:

    Close all windows except the main one and print the main window's title -> Option B
  4. Quick Check:

    Close others, keep main, print main title [OK]
Hint: Close non-main windows, then switch back [OK]
Common Mistakes:
  • Closing main window inside the loop
  • Not switching back to main window before printing title
  • Assuming all windows are closed
4. Identify the error in this Selenium Java code snippet for switching to a new window:
Set<String> windows = driver.getWindowHandles();
for (String win : windows) {
    if (win != driver.getWindowHandle()) {
        driver.switchTo().window(win);
        break;
    }
}
medium
A. Using '!=' to compare strings instead of .equals()
B. Missing driver.quit() after switching windows
C. Not closing the previous window before switching
D. Using getWindowHandles() instead of getWindowHandle()

Solution

  1. Step 1: Check string comparison method

    In Java, strings must be compared with .equals(), not '!=' which compares references.
  2. Step 2: Identify impact of wrong comparison

    Using '!=' may cause the condition to fail unexpectedly, so the switch may not happen correctly.
  3. Final Answer:

    Using '!=' to compare strings instead of .equals() -> Option A
  4. Quick Check:

    Use .equals() for string comparison [OK]
Hint: Always use .equals() to compare strings in Java [OK]
Common Mistakes:
  • Using '==' or '!=' for string comparison
  • Forgetting to break after switching
  • Confusing getWindowHandle() and getWindowHandles()
5. You have a test that opens multiple tabs. You want to switch to the tab with title "Dashboard" and perform actions there. Which approach correctly switches to that window?
hard
A. Close all windows except the first, then assume it is "Dashboard"
B. Use driver.switchTo().window("Dashboard"); directly with the title
C. Call driver.getWindowHandle() repeatedly until title is "Dashboard"
D. Iterate all window handles, switch to each, check title, break when title matches "Dashboard"

Solution

  1. Step 1: Understand window switching by handle

    You cannot switch windows by title directly; you must switch by handle and then check the title.
  2. Step 2: Iterate handles and check titles

    Loop through all window handles, switch to each, check if title equals "Dashboard", then break to stay on that window.
  3. Final Answer:

    Iterate all window handles, switch to each, check title, break when title matches "Dashboard" -> Option D
  4. Quick Check:

    Switch by handle, check title, then stop [OK]
Hint: Switch windows by handle, verify title, then stop [OK]
Common Mistakes:
  • Trying to switch using title as window handle
  • Not looping through all windows
  • Assuming first window is always the target
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard