- Selenium - Home
- Selenium - Overview
- Selenium - Components
- Selenium - Automation Testing
- Selenium - Environment Setup
- Selenium - Remote Control
- Selenium - IDE Introduction
- Selenium - Features
- Selenium - Limitations
- Selenium - Installation
- Selenium - Creating Tests
- Selenium - Creating Script
- Selenium - Control Flow
- Selenium - Store Variables
- Selenium - Alerts & Popups
- Selenium - Selenese Commands
- Selenium - Actions Commands
- Selenium - Accessors Commands
- Selenium - Assertions Commands
- Selenium - Assert/Verify Methods
- Selenium - Locating Strategies
- Selenium - Script Debugging
- Selenium - Verification Points
- Selenium - Pattern Matching
- Selenium - JSON Data File
- Selenium - Browser Execution
- Selenium - User Extensions
- Selenium - Code Export
- Selenium - Emitting Code
- Selenium - JavaScript Functions
- Selenium - Plugins
- Selenium WebDriver Tutorial
- Selenium - Introduction
- Selenium WebDriver vs RC
- Selenium - Installation
- Selenium - First Test Script
- Selenium - Driver Sessions
- Selenium - Browser Options
- Selenium - Chrome Options
- Selenium - Edge Options
- Selenium - Firefox Options
- Selenium - Safari Options
- Selenium - Double Click
- Selenium - Right Click
- HTML Report in Python
- Handling Edit Boxes
- Selenium - Single Elements
- Selenium - Multiple Elements
- Selenium Web Elements
- Selenium - File Upload
- Selenium - Locator Strategies
- Selenium - Relative Locators
- Selenium - Finders
- Selenium - Find All Links
- Selenium - User Interactions
- Selenium - WebElement Commands
- Selenium - Browser Interactions
- Selenium - Browser Commands
- Selenium - Browser Navigation
- Selenium - Alerts & Popups
- Selenium - Handling Forms
- Selenium - Windows and Tabs
- Selenium - Handling Links
- Selenium - Input Boxes
- Selenium - Radio Button
- Selenium - Checkboxes
- Selenium - Dropdown Box
- Selenium - Handling IFrames
- Selenium - Handling Cookies
- Selenium - Date Time Picker
- Selenium - Dynamic Web Tables
- Selenium - Actions Class
- Selenium - Action Class
- Selenium - Keyboard Events
- Selenium - Key Up/Down
- Selenium - Copy and Paste
- Selenium - Handle Special Keys
- Selenium - Mouse Events
- Selenium - Drag and Drop
- Selenium - Pen Events
- Selenium - Scroll Operations
- Selenium - Waiting Strategies
- Selenium - Explicit/Implicit Wait
- Selenium - Support Features
- Selenium - Multi Select
- Selenium - Wait Support
- Selenium - Select Support
- Selenium - Color Support
- Selenium - ThreadGuard
- Selenium - Errors & Logging
- Selenium - Exception Handling
- Selenium - Miscellaneous
- Selenium - Handling Ajax Calls
- Selenium - JSON Data File
- Selenium - CSV Data File
- Selenium - Excel Data File
- Selenium - Cross Browser Testing
- Selenium - Multi Browser Testing
- Selenium - Multi Windows Testing
- Selenium - JavaScript Executor
- Selenium - Headless Execution
- Selenium - Capture Screenshots
- Selenium - Capture Videos
- Selenium - Page Object Model
- Selenium - Page Factory
- Selenium - Record & Playback
- Selenium - Frameworks
- Selenium - Browsing Context
- Selenium - DevTools
- Selenium Grid Tutorial
- Selenium - Overview
- Selenium - Architecture
- Selenium - Components
- Selenium - Configuration
- Selenium - Create Test Script
- Selenium - Test Execution
- Selenium - Endpoints
- Selenium - Customizing a Node
- Selenium Reporting Tools
- Selenium - Reporting Tools
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium & Other Technologies
- Selenium - Java Tutorial
- Selenium - Python Tutorial
- Selenium - C# Tutorial
- Selenium - Javascript Tutorial
- Selenium - Kotlin Tutorial
- Selenium - Ruby Tutorial
- Selenium - Maven & Jenkins
- Selenium - LogExpert Logging
- Selenium - Log4j Logging
- Selenium - Robot Framework
- Selenium - Github Tutorial
- Selenium - IntelliJ
- Selenium - XPath
- Selenium Miscellaneous Concepts
- Selenium - IE Driver
- Selenium - Automation Frameworks
- Selenium - Keyword Driven Framework
- Selenium - Data Driven Framework
- Selenium - Hybrid Driven Framework
- Selenium - SSL Certificate Error
- Selenium - Alternatives
Selenium WebDriver - Multi Windows Testing
Selenium Webdriver can be used to handle multi-windows testing. All windows launched are identified by a unique identifier for a session. As another window opens, the context of the driver continues on the parent window. To execute tasks on a child window, the context of the driver needs to be shifted from the parent to the child.
Basic Methods to Handle Multiple Windows in Selenium
There are various methods in Selenium that can be used to automate tests dealing with multiple windows. To work on the child windows, the driver context needs to be shifted from the parent window to the child windows.
Example 1
In the below page, click on New Tab.
Post clicking the New Tab, we would move to another tab with text New Tab.
Code Implementation
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class TabsHandling {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new EdgeDriver();
// adding implicit wait of 15 secs
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// Opening the webpage with new tab
driver.get("https://www.tutorialspoint.com/selenium/practice/browser-windows.php");
// click link then navigate to next tab
WebElement b = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/button[1]"));
b.click();
// Get original window handle id
String oW = driver.getWindowHandle();
// get every windows handle ids
Set<String> windows = driver.getWindowHandles();
// loop through all window handles
for (String w : windows) {
if(!oW.equalsIgnoreCase(w)) {
// switch to the child tab
driver.switchTo().window(w);
// get element in new tab
WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/h1"));
System.out.println("Text in new tab: " + e.getText());
break;
}
}
// quit the browser
driver.quit();
}
}
Output
Text in new tab is: New Tab Process finished with exit code 0
Here, we had obtained the text on the newly opened tab and got the message in the console - Text in new tab: New Tab.
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
Example 2
In the below page, click on New Window Message.
Post clicking the New Window Message, we would move to another window with text New Window Message.
Close the new window, shift back to the original window, and obtain the text - Browser Windows there. At last, quit the session.
Code Implementation
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class WindowsOpen {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new EdgeDriver();
// adding implicit wait of 20 secs
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
// Opening the webpage to open a new window
driver.get("https://www.tutorialspoint.com/selenium/practice/browser-windows.php");
// click link to next window
WebElement b = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/button[3]"));
b.click();
// Obtain original window handle id
String oW = driver.getWindowHandle();
// obtain all opened windows handle ids
Set<String> windows = driver.getWindowHandles();
// Loop through all window handles
for (String w : windows) {
if(!oW.equalsIgnoreCase(w)) {
// switch to child window
driver.switchTo().window(w);
// get element in new window
WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/h1"));
System.out.println("Text in new window: " + e.getText());
driver.close();
break;
}
}
// switch to parent window
driver.switchTo().window(oW);
// get element in parent window
WebElement e1 = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/h1"));
System.out.println("Text in parent window: " + e1.getText());
// quit browser session
driver.quit();
}
}
Output
Text in new window: New Window Message Text in parent window: Browser Windows
Here, we had captured text on the new window and got the message in the console - Text in new window: New Window Message. Then, we ended the child window and moved back to the parent window. At last, we had obtained text on the parent window in the console - Text in parent window: Browser Windows.
Thus there is a minor difference between close() and quit() methods. The close() method only closes the active browser window, and the quit() method terminates all the opened browser windows at the same time.
Example 3
From the version 4, we can open a new window using the below method −
driver.switchTo().newWindow(WindowType.WINDOW)
Open an application in a browser window, and obtain text - Check Box as shown in the below image −
Then open another new window and launch a different application with text - Radio Button.
Code Implementation
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.edge.EdgeDriver;
import java.util.concurrent.TimeUnit;
public class NewWindowOpen {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new EdgeDriver();
// adding implicit wait of 12 secs
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
// Open a webpage in first window
driver.get("https://www.tutorialspoint.com/selenium/practice/check-box.php");
// get text in first window
WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/h1"));
System.out.println("Text: " + e.getText());
// Initiate the another Webdriver
WebDriver newDriver = driver.switchTo().newWindow(WindowType.WINDOW);
// Opening another webpage in second window
driver.get("https://www.tutorialspoint.com/selenium/practice/radio-button.php");
// get text in second window
WebElement e1 = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/form/h1"));
System.out.println("Text in new window: " + e1.getText());
// quit the browser session
driver.quit();
}
}
Output
Text: Check Box Text in new window: Radio Button
Here, we had captured text in the first window with the message in the console - Text: Check Box. Then opened another new window to launch an application there. At last, we had obtained text in the new window with a message in the console - Text in new window: Radio Button.
Example 4
From the version 4, we can open a new tab using the below method −
driver.switchTo().newWindow(WindowType.TAB)
Open an application in a browser, and obtain text - Check Box as shown in the below image −
Then open another new tab and launch a different application with text - Radio Button.
Code Implementation
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.edge.EdgeDriver;
import java.util.concurrent.TimeUnit;
public class NewTabsOpen {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new EdgeDriver();
// adding implicit wait of 15 secs
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// Open a webpage in first window
driver.get("https://www.tutorialspoint.com/selenium/practice/check-box.php");
// get text in first window
WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/h1"));
System.out.println("Text: " + e.getText());
// Initiate the another Webdriver
WebDriver newDriver = driver.switchTo().newWindow(WindowType.TAB);
// Open a webpage in new tab
driver.get("https://www.tutorialspoint.com/selenium/practice/radio-button.php");
// obtain text in other tab
WebElement e1 = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/form/h1"));
System.out.println("Text in other tab: " + e1.getText());
// quit the browser session
driver.quit();
}
}
Output
Text: Check Box Text in other tab: Radio Button
Here, we had captured the text in the first window with the message in the console - Text: Check Box. Then opened another new tab to launch an application there. Atlast, we obtained text in the new tab with the message in the console - Text in other tab: Radio Button.
Conclusion
This concludes our comprehensive take on the tutorial on Selenium Webdriver Multi Windows Testing. Weve started with describing basic methods to handle multiple windows in Selenium, and walked through examples on how to handle multiple windows with Selenium Webdriver. This equips you with in-depth knowledge of the Selenium Webdriver - Multi Windows Testing. It is wise to keep practicing what youve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.