Windows & Tabs
Practice handling parent windows, child windows, browser tabs and window switching using Selenium WebDriver.
What You Will Practice
- Capture the parent window handle.
- Get all available window handles.
- Open and switch to a child window.
- Work with multiple browser tabs.
- Return to the parent window.
- Close a child window safely.
Parent Window
Capture and identify the current browser window handle.
String parentWindow = driver.getWindowHandle();
Open Child Window
Open a new browser window and identify the new window.
driver.switchTo().newWindow(WindowType.WINDOW);
Multiple Windows
Create multiple browser windows and inspect their window handles.
Set<String> windows = driver.getWindowHandles();
Browser Tabs
Practice opening and identifying a new browser tab.
driver.switchTo().newWindow(WindowType.TAB);
Switch Between Windows
Practice selecting a specific window from the available handles.
driver.switchTo().window(windowHandle);
Close Child & Return
Practice closing a child window and switching back to the parent.
driver.close(); driver.switchTo().window(parentWindow);
Selenium Windows & Tabs Commands
String parent =
driver.getWindowHandle();
Set<String> windows =
driver.getWindowHandles();
driver.switchTo()
.window(windowHandle);
driver.switchTo()
.newWindow(WindowType.WINDOW);
driver.switchTo()
.newWindow(WindowType.TAB);
driver.close();