Selenium WebDriver
Day 27: Multiple Window & Tab Handling in Selenium
Master window and tab handling techniques using getWindowHandle(), getWindowHandles(), and switchTo().window() methods
Today's Learning Objectives
Window Handling Fundamentals
- • Understanding multiple windows and tabs
- • Window ID capture and management
- • Switching between windows efficiently
- • Set to ArrayList conversion techniques
Practical Applications
- • Multi-application workflow automation
- • Cross-window data transfer
- • Complex navigation scenarios
- • Real-world assignment challenges
Window Handling Methods
Core Window Handling Methods
1. getWindowHandle() Method
Captures the current window ID number (single window)
String mainWindowId = driver.getWindowHandle();
2. getWindowHandles() Method
Captures all window ID numbers (multiple windows)
Set<String> allWindowID = driver.getWindowHandles();
3. switchTo().window() Method
Switches focus to the expected window using window ID
driver.switchTo().window("window ID");
Window Handling Process
Step-by-Step Process
Step 1: Capture Main Window ID
String mainWindow = driver.getWindowHandle();
Step 2: Perform Operations & Open New Window
driver.findElement(By.name("fname")).sendKeys("Rahul");
driver.findElement(By.name("lname")).sendKeys("Patil");
driver.findElement(By.xpath("//a[text()='SauceDemo Link']")).click();
driver.findElement(By.name("lname")).sendKeys("Patil");
driver.findElement(By.xpath("//a[text()='SauceDemo Link']")).click();
Step 3: Capture All Window IDs
Set<String> allWindow = driver.getWindowHandles();
Step 4: Convert Set to ArrayList
ArrayList<String> allArrayWindow = new ArrayList<String>(allWindow);
This allows us to access window IDs by index position
Step 5: Get Specific Window ID & Switch
String sauceWindowId = allArrayWindow.get(1);
driver.switchTo().window(sauceWindowId);
driver.switchTo().window(sauceWindowId);
Example 1: Two Window Handling
Task: Main Window + SauceDemo Window
Website: https://prafpawar11.github.io/multiplewindows.html
Complete Implementation
Java Code Implementation
ChromeDriver driver = new ChromeDriver();
driver.get("https://prafpawar11.github.io/multiplewindows.html");
// Fill main window form
driver.findElement(By.name("fname")).sendKeys("Rahul");
driver.findElement(By.name("lname")).sendKeys("Pandit");
driver.findElement(By.xpath("//a[text()='SauceDemo Link']")).click();
// Capture all window IDs
Set<String> allSetWindowIds = driver.getWindowHandles();
ArrayList<String> allArrayWindowIds = new ArrayList<String>(allSetWindowIds);
// Switch to SauceDemo window
String sauceWindowId = allArrayWindowIds.get(1);
driver.switchTo().window(sauceWindowId);
// SauceDemo credentials
driver.findElement(By.name("user-name")).sendKeys("standard_user");
driver.findElement(By.name("password")).sendKeys("secret_sauce");
driver.findElement(By.name("login-button")).click();
// Switch back to main window
String mainWindow = allArrayWindowIds.get(0);
driver.switchTo().window(mainWindow);
driver.findElement(By.name("address")).sendKeys("Pune");
driver.get("https://prafpawar11.github.io/multiplewindows.html");
// Fill main window form
driver.findElement(By.name("fname")).sendKeys("Rahul");
driver.findElement(By.name("lname")).sendKeys("Pandit");
driver.findElement(By.xpath("//a[text()='SauceDemo Link']")).click();
// Capture all window IDs
Set<String> allSetWindowIds = driver.getWindowHandles();
ArrayList<String> allArrayWindowIds = new ArrayList<String>(allSetWindowIds);
// Switch to SauceDemo window
String sauceWindowId = allArrayWindowIds.get(1);
driver.switchTo().window(sauceWindowId);
// SauceDemo credentials
driver.findElement(By.name("user-name")).sendKeys("standard_user");
driver.findElement(By.name("password")).sendKeys("secret_sauce");
driver.findElement(By.name("login-button")).click();
// Switch back to main window
String mainWindow = allArrayWindowIds.get(0);
driver.switchTo().window(mainWindow);
driver.findElement(By.name("address")).sendKeys("Pune");
Key Points
- • Index 0 = Main window (first opened)
- • Index 1 = Second window (SauceDemo)
- • Always convert Set to ArrayList for index access
- • Switch back to main window using index 0
Example 2: Multiple Window Handling
Task: Main + OrangeHRM + Cogmento Windows
Workflow Overview
1. Main Window → Enter first name → Click OrangeHRM link
2. OrangeHRM Window → Login with credentials
3. Back to Main → Enter last name → Click Cogmento link
4. Cogmento Window → Login with credentials
5. Back to Main → Enter address
Key Implementation Points
Window Index Management
// After opening 3 windows:
String mainWindowId = allArrayWindowIds.get(0); // Main
String orangeWindowId = allArrayWindowIds.get(1); // OrangeHRM
String cogmentoWindowId = allArrayWindowIds.get(2); // Cogmento
String mainWindowId = allArrayWindowIds.get(0); // Main
String orangeWindowId = allArrayWindowIds.get(1); // OrangeHRM
String cogmentoWindowId = allArrayWindowIds.get(2); // Cogmento
Re-capturing Window Handles
// Re-assign after opening new window
allSetWindowIds = driver.getWindowHandles();
allArrayWindowIds = new ArrayList<String>(allSetWindowIds);
allSetWindowIds = driver.getWindowHandles();
allArrayWindowIds = new ArrayList<String>(allSetWindowIds);
Real-World Applications
Common Use Cases
E-commerce Testing
- • Product comparison across tabs
- • Payment gateway redirections
- • Social media login popups
- • Help/Support chat windows
Banking Applications
- • Fund transfer confirmations
- • Document upload windows
- • OTP verification popups
- • Statement download tabs
Enterprise Applications
- • Multi-system integrations
- • Report generation windows
- • External tool integrations
- • Dashboard popup windows
Social Media Testing
- • Share content across platforms
- • Login with social accounts
- • Media upload windows
- • External link validations
Comprehensive Assignment
Multi-Application Workflow Challenge
Website: https://prafpawar11.github.io/multiplewindows.html
Phase 1: Initial Setup
- • Enter first name → Click SauceDemo link → Login
- • Return to main → Enter last name → Click OrangeHRM link → Login
- • Return to main → Enter address → Click Cogmento CRM → Login
Phase 2: SauceDemo Operations
- • Switch to SauceDemo → Select "High to Low" price filter
- • Add highest price item to cart → Complete checkout process
- • Enter personal information → Click finish button
Phase 3: Cogmento CRM Operations
- • Switch to Cogmento → Click Contacts → Create new contact
- • Enter first name, last name, select status
- • Select TikTok as social channel → Save contact
- • Delete created contact → Logout application
Phase 4: OrangeHRM Operations
- • Switch to OrangeHRM → PIM → Add Employee
- • Enter employee details → Capture employee ID
- • Set nationality, gender, DOB → Save
- • Admin → Add user → Create employee credentials
- • Logout → Login with new employee credentials
Best Practices for Window Handling
✅ Do's
- • Always capture main window ID before opening new windows
- • Convert Set to ArrayList for index-based access
- • Re-capture window handles after opening new windows
- • Use meaningful variable names for window IDs
- • Add explicit waits after window switches
- • Close unused windows to free memory
❌ Don'ts
- • Don't assume window order will remain constant
- • Avoid hardcoding window indices without verification
- • Don't forget to switch back to main window
- • Avoid performing operations without switching context
- • Don't leave multiple windows open unnecessarily
- • Avoid using Thread.sleep() for window loading
Knowledge Check
Question 1 of 5
Which method is used to capture the current window ID in Selenium?
Key Points Summary
Window Handling Methods
- • getWindowHandle(): Current window ID
- • getWindowHandles(): All window IDs
- • switchTo().window(): Switch between windows
- • Set to ArrayList: Index-based access
Best Practices
- • Capture main window ID first
- • Re-capture handles after new windows
- • Use meaningful variable names
- • Always switch back to main window