Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Selenium Automation Testing

  • Introduction to Automation Testing
  •  Introduction to Selenium
  • Advantages and Disadvantages of Selenium WebDriver
  •  Locators in Selenium
  • How to launch the browsers ? 
  • Browser Navigation Commands in Selenium
  •  Handling the textbox and button using selenium
  • Handling the radio button and checkbox using selenium
  •  Handling the drop downs and multi-select dropdown
  • Validate the text validation and attributes values. 
  • Handling Alert Popup
  • Handling iFrames
  • Handling Multiple Browsers or Tabs
View Categories
  • Home
  • Tutorials
  • Selenium Automation
  • Selenium Automation Testing
  • Handling Multiple Browsers or Tabs

Handling Multiple Browsers or Tabs

Handling Multiple Browsers or Tabs

How it works:

1. Setup & launch – set webdriver.chrome.driver and open a Chrome session.

2. Navigate – go to your test page (replace the URL).

3. Main handle – capture driver.getWindowHandle() for returning later.

4. Trigger – click the element that opens a new window.

5. Handles set – call driver.getWindowHandles() to get both main and child handles.

6. Switch – iterate the set, find the handle that isn’t the main one, and driver.switchTo().window(childHandle).

7. Interact – perform any reads or clicks in the child window.

8. Close child – driver.close() closes only the current window.

9. Return – driver.switchTo().window(mainWindowHandle) brings you back to the original window.

10. Teardown – driver.quit() ensures all windows are closed and the session ends.

Adjust the locators (By.xpath(…)), URLs, and any child-window interactions to match your application under test.

Program:

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import java.util.Set;

public class WindowHandlingExample {

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

        // 1. Point to your ChromeDriver executable

        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        // 2. Launch Chrome

        WebDriver driver = new ChromeDriver();

        try {

            // 3. Navigate to the page with a link/button that opens a new window

            driver.get("https://example.com/multiple-windows");

            // 4. Store the handle of the main window

            String mainWindowHandle = driver.getWindowHandle();

            System.out.println("Main window handle: " + mainWindowHandle);

            // 5. Trigger opening a new window (adjust locator as needed)

            WebElement openNewWindowBtn = driver.findElement(

                By.xpath("//button[@id='openWindow']"));

            openNewWindowBtn.click();

            // 6. Wait briefly for the new window to open

            Thread.sleep(2000);

            // 7. Get all window handles

            Set<String> allWindowHandles = driver.getWindowHandles();

            System.out.println("All window handles: " + allWindowHandles);

            // 8. Switch to the newly opened window

            for (String handle : allWindowHandles) {

                if (!handle.equals(mainWindowHandle)) {

                    driver.switchTo().window(handle);

                    System.out.println("Switched to child window: " + handle);

                    break;

                }

            }

            // 9. Perform actions in the child window

            System.out.println("Child window title: " + driver.getTitle());

            // e.g., click something or read some text

            // WebElement childElement = driver.findElement(By.id("someElement"));

            // System.out.println(childElement.getText());

            // 10. Close the child window

            driver.close();

            System.out.println("Child window closed.");

            // 11. Switch back to the main window

            driver.switchTo().window(mainWindowHandle);

            System.out.println("Switched back to main window: " + driver.getTitle());

        } finally {

            // 12. Close all browser windows and end the session

            driver.quit();

        }

    }

}

Selenium Automation Testing
What are your Feelings
Share This Article :
  • Facebook
  • X
  • LinkedIn
Handling iFrames
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105