Handling Multiple Windows - Selenium Webdriver Java

To handle multiple windows effectively, the Selenium WebDriver Java provides several methods and techniques:

  1. Window Handles: Each window or tab in the browser is assigned a unique identifier called a window handle. WebDriver maintains a set of window handles, allowing you to switch between different windows or tabs. You can obtain the current window handle using the driver.getWindowHandle() method, and retrieve all window handles using driver.getWindowHandles().

  2. Switching between Windows: The driver.switchTo().window() method enables you to switch the focus of WebDriver to a different window or tab. You need to provide the window handle as an argument to this method. By switching to the desired window, you can interact with the elements present within that window.

  3. Managing Pop-up Windows: When a pop-up window appears, you can use the driver.getWindowHandles() method to retrieve all the window handles. Once you have the handles, you can iterate through them to switch to the appropriate window using the driver.switchTo().window() method. After interacting with the pop-up window, you can switch back to the main window using the same approach.

  4. Verifying Window Titles and URLs: To ensure that the correct window is being accessed, you can verify the window title or URL. You can retrieve the title using driver.getTitle() and the current URL using driver.getCurrentUrl(). Comparing these values with the expected values allows you to validate if you have switched to the intended window.

  5. Closing Windows: You can close the current window or tab using driver.close(). If the current window is the main window, it will be closed, and the focus will be shifted to the next available window. To close a specific window, you need to switch to it using driver.switchTo().window() and then call driver.close().

Below is the code snippet using one simple scenario:

package com.ui.test;

import java.time.Duration; import java.util.Iterator; import java.util.Set;

import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait;

public class HandlingMultipleWindows {

public static void main(String[] args) throws InterruptedException {

WebDriver driver = new ChromeDriver(); driver.get("https://www.lambdatest.com/selenium-playground/window-popup-modal-demo"); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));

WebElement signupLink = driver.findElement(By.xpath("//a[contains(text(),'Sign Up')]"));

String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.ENTER); signupLink.sendKeys(selectLinkOpeninNewTab);

String mainWindow = driver.getWindowHandle();

Set allWindows = driver.getWindowHandles();

Iterator iterator = allWindows.iterator();

while (iterator.hasNext()) { String currentWondow = iterator.next();

if (!mainWindow.equals(currentWondow)) {

WebDriver window = driver.switchTo().window(currentWondow); String pageURL = window.getCurrentUrl(); System.out.println("pageURL:" + pageURL);

driver.findElement(By.xpath("//button[contains(text(),'SIGN UP')]")).click(); Thread.sleep(5000); driver.close(); Thread.sleep(5000); } }

String currentUrl = driver.switchTo().window(mainWindow).getCurrentUrl(); System.out.println("currentURL:"+currentUrl); //The code below will open an empty new tab.

WebElement signupLink1 = driver.findElement(By.xpath("//a[contains(text(),'Sign Up')]"));

String selectLinkOpeninNewTab1 = Keys.chord(Keys.CONTROL,Keys.TAB);

signupLink1.sendKeys(selectLinkOpeninNewTab1);

Thread.sleep(5000);

//driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);

driver.quit(); } }