Bird
Raised Fist0
Selenium Javatesting~20 mins

Window handles (getWindowHandles) in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Window Handles 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 getWindowHandles() after opening a new tab?

Consider the following Selenium Java code snippet:

WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
String originalHandle = driver.getWindowHandle();
((JavascriptExecutor)driver).executeScript("window.open('https://google.com','_blank');");
Set<String> handles = driver.getWindowHandles();
System.out.println(handles.size());

What will be printed?

Selenium Java
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
String originalHandle = driver.getWindowHandle();
((JavascriptExecutor)driver).executeScript("window.open('https://google.com','_blank');");
Set<String> handles = driver.getWindowHandles();
System.out.println(handles.size());
A1
B0
C2
DThrows NoSuchWindowException
Attempts:
2 left
💡 Hint

Remember that getWindowHandles() returns all open windows and tabs.

❓ assertion
intermediate
1:30remaining
Which assertion correctly verifies the number of open windows?

You want to assert that exactly three browser windows are open after some actions. Which assertion is correct?

Selenium Java
Set<String> handles = driver.getWindowHandles();
AassertNull(handles.size() - 3);
BassertEquals(3, handles.size());
CassertFalse(handles.size() != 3);
DassertTrue(handles.size() == 3);
Attempts:
2 left
💡 Hint

Use the assertion that clearly compares expected and actual values.

🔧 Debug
advanced
2:30remaining
Why does switching to a window handle cause NoSuchWindowException?

Given this code snippet:

Set<String> handles = driver.getWindowHandles();
for (String handle : handles) {
    driver.switchTo().window(handle);
    driver.close();
}
driver.switchTo().window(handles.iterator().next());

Why does the last line throw NoSuchWindowException?

ABecause all windows were closed in the loop, so the handle is invalid.
BBecause <code>handles.iterator().next()</code> returns null after closing windows.
CBecause <code>switchTo()</code> requires a frame, not a window handle.
DBecause <code>driver.close()</code> does not close windows, only tabs.
Attempts:
2 left
💡 Hint

Think about what happens to window handles after closing windows.

🧠 Conceptual
advanced
1:30remaining
What is the main difference between getWindowHandle() and getWindowHandles()?

Choose the best explanation:

A<code>getWindowHandle()</code> returns the handle of the current window; <code>getWindowHandles()</code> returns handles of all open windows/tabs.
B<code>getWindowHandle()</code> returns all window handles; <code>getWindowHandles()</code> returns the current window handle.
CBoth methods return the same set of window handles.
D<code>getWindowHandle()</code> returns handles of all frames; <code>getWindowHandles()</code> returns handles of all windows.
Attempts:
2 left
💡 Hint

Think about singular vs plural in the method names.

❓ framework
expert
3:00remaining
How to safely switch to a newly opened window in Selenium Java?

You have a test that opens a new window. Which code snippet correctly switches to the new window without errors?

Selenium Java
String originalHandle = driver.getWindowHandle();
// code that opens new window
Set<String> allHandles = driver.getWindowHandles();
Adriver.switchTo().frame(allHandles.iterator().next());
Bdriver.switchTo().window(allHandles.iterator().next());
Cdriver.switchTo().window(originalHandle);
D
for (String handle : allHandles) {
  if (!handle.equals(originalHandle)) {
    driver.switchTo().window(handle);
    break;
  }
}
Attempts:
2 left
💡 Hint

Remember to avoid switching back to the original window.

Practice

(1/5)
1. What does the Selenium method getWindowHandles() return when called?
easy
A. A set of all open window handles (IDs) in the current WebDriver session
B. The handle of the current active window only
C. The title of the current browser window
D. The URL of the current page

Solution

  1. Step 1: Understand the purpose of getWindowHandles()

    This method is designed to return all window handles for every open window or tab in the current browser session.
  2. Step 2: Identify the return type

    The method returns a Set of Strings, each representing a unique window handle (ID).
  3. Final Answer:

    A set of all open window handles (IDs) in the current WebDriver session -> Option A
  4. Quick Check:

    getWindowHandles() = all window IDs [OK]
Hint: Remember: getWindowHandles() returns all window IDs as a set [OK]
Common Mistakes:
  • Confusing getWindowHandles() with getWindowHandle() which returns one handle
  • Expecting a list or array instead of a set
  • Thinking it returns page titles or URLs
2. Which of the following is the correct syntax to get all window handles in Selenium Java?
easy
A. List<String> handles = driver.getWindowHandles();
B. String handles = driver.getWindowHandles();
C. Set<String> handles = driver.getWindowHandles();
D. Set handles = driver.getWindowHandle();

Solution

  1. Step 1: Check the return type of getWindowHandles()

    The method returns a Set of Strings, so the variable must be declared as Set<String>.
  2. Step 2: Verify method name and syntax

    The correct method is getWindowHandles() (plural), and it belongs to the WebDriver instance named driver.
  3. Final Answer:

    Set<String> handles = driver.getWindowHandles(); -> Option C
  4. Quick Check:

    Correct syntax uses Set<String> and getWindowHandles() [OK]
Hint: Use Set<String> for getWindowHandles() return type [OK]
Common Mistakes:
  • Using List instead of Set for window handles
  • Calling getWindowHandle() instead of getWindowHandles()
  • Assigning to a String instead of a collection
3. Consider the following code snippet:
Set<String> handles = driver.getWindowHandles();
System.out.println(handles.size());

If the browser has 3 tabs open, what will be printed?
medium
A. An exception is thrown
B. 1
C. 0
D. 3

Solution

  1. Step 1: Understand what getWindowHandles() returns

    It returns a set containing all window handles for every open tab or window.
  2. Step 2: Determine the size of the set

    Since there are 3 tabs open, the set will contain 3 unique handles, so size() returns 3.
  3. Final Answer:

    3 -> Option D
  4. Quick Check:

    Number of tabs = size of handles set = 3 [OK]
Hint: Size of getWindowHandles() set equals number of open tabs/windows [OK]
Common Mistakes:
  • Assuming size() returns 1 for current window only
  • Expecting an exception if multiple tabs exist
  • Confusing getWindowHandles() with getWindowHandle()
4. You wrote this code to switch to a new window:
Set<String> handles = driver.getWindowHandles();
String newWindow = handles.iterator().next();
driver.switchTo().window(newWindow);

But it always switches to the original window. What is the likely problem?
medium
A. getWindowHandles() returns an empty set
B. Using iterator().next() returns the first handle, which is the original window
C. switchTo().window() requires window title, not handle
D. You must convert the set to a list before switching

Solution

  1. Step 1: Analyze iterator().next() behavior on a Set

    Calling iterator().next() on a Set returns the first element, which is usually the original window handle.
  2. Step 2: Understand why it switches to original window

    Since newWindow is assigned the original window handle, switching to it does not change the window.
  3. Final Answer:

    Using iterator().next() returns the first handle, which is the original window -> Option B
  4. Quick Check:

    iterator().next() = first handle (original window) [OK]
Hint: iterator().next() returns first handle, often original window [OK]
Common Mistakes:
  • Assuming iterator().next() gives the new window handle
  • Thinking switchTo().window() needs window title
  • Not checking if getWindowHandles() is empty
5. 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?
hard
A. for (String handle : allHandles) { if (!handle.equals(originalHandle)) { driver.switchTo().window(handle); break; } }
B. driver.switchTo().window(allHandles.iterator().next());
C. driver.switchTo().window(originalHandle);
D. driver.switchTo().window(allHandles.toArray()[0].toString());

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]
Hint: 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
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