0
0
Selenium Javatesting~10 mins

Switching between windows in Selenium Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to switch to the current browser window.

Selenium Java
driver.switchTo().window([1]);
Drag options to blanks, or click blank then click option'
Adriver.close()
Bdriver.getWindowHandles()
Cdriver.getWindowHandle()
Ddriver.switchTo()
Attempts:
3 left
💡 Hint
Common Mistakes
Using getWindowHandles() which returns a set of handles, not a single one.
Using switchTo() inside the parameter which is incorrect.
2fill in blank
medium

Complete the code to get all window handles after opening a new tab.

Selenium Java
Set<String> windowHandles = driver.[1]();
Drag options to blanks, or click blank then click option'
AgetWindowHandles
Bclose
CgetWindowHandle
DswitchTo
Attempts:
3 left
💡 Hint
Common Mistakes
Using getWindowHandle which returns only one handle.
Trying to call switchTo() without parentheses.
3fill in blank
hard

Fix the error in the code to switch to the second window handle.

Selenium Java
for (String handle : driver.getWindowHandles()) {
    if (!handle.equals([1])) {
        driver.switchTo().window(handle);
        break;
    }
}
Drag options to blanks, or click blank then click option'
Adriver.getWindowHandle()
Bdriver.getWindowHandles()
Cdriver.switchTo()
Dhandle
Attempts:
3 left
💡 Hint
Common Mistakes
Using getWindowHandles() which returns a set, not a single handle.
Using switchTo() inside the equals comparison.
4fill in blank
hard

Fill both blanks to correctly switch back to the original window after closing the new one.

Selenium Java
String originalWindow = driver.[1]();
// After closing new window
 driver.switchTo().[2](originalWindow);
Drag options to blanks, or click blank then click option'
AgetWindowHandle
BgetWindowHandles
Cwindow
DwindowHandle
Attempts:
3 left
💡 Hint
Common Mistakes
Using getWindowHandles() for the original window handle.
Using getWindowHandle() instead of window() in switchTo.
5fill in blank
hard

Fill all three blanks to create a map of window titles keyed by their handles.

Selenium Java
Map<String, String> windowTitles = new HashMap<>();
for (String handle : driver.[1]()) {
    driver.switchTo().[2](handle);
    windowTitles.put(handle, driver.[3]());
}
Drag options to blanks, or click blank then click option'
AgetWindowHandles
Bwindow
CgetTitle
DgetWindowHandle
Attempts:
3 left
💡 Hint
Common Mistakes
Using getWindowHandle() instead of getWindowHandles() for the loop.
Using switchTo().getTitle() which is invalid.
Confusing window() and getWindowHandle() methods.