Saturday, December 5, 2026

Selenium 4 New Window in Java

Selenium 4 New Window in Java

Selenium 4 introduced a new method called newWindow() in the TargetLocator interface. It allows you to open a new browser window or tab directly without using JavaScript.

Before Selenium 4, we commonly used JavaScriptExecutor to open a new browser window or tab.

Selenium 4 New Window in Java


1. Syntax of newWindow()

driver.switchTo().newWindow(WindowType.TAB);

To open a new browser window:

driver.switchTo().newWindow(WindowType.WINDOW);

There are two supported window types:

Window Type Description
WindowType.TAB Opens a new browser tab
WindowType.WINDOW Opens a new browser window

2. Example: Open a New Tab in Selenium 4

package org.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WindowType;

public class NewWindowExample {

    public static void main(String[] args) {

        WebDriver driver = new ChromeDriver();

        // Open first website
        driver.get("https://www.google.com");

        // Print current window title
        System.out.println("First Window: " + driver.getTitle());

        // Open a new tab
        driver.switchTo().newWindow(WindowType.TAB);

        // Navigate to another website
        driver.get("https://www.amazon.com");

        // Print second window title
        System.out.println("Second Window: " + driver.getTitle());

        // Close browser
        driver.quit();
    }
}

Explanation:

  1. Launch Chrome browser.
  2. Open Google in the first tab.
  3. Use newWindow(WindowType.TAB) to open a new tab.
  4. Selenium automatically switches control to the newly opened tab.
  5. Open Amazon in the new tab.
  6. Close all browser windows using quit().

3. Example: Open a New Browser Window

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WindowType;

public class NewBrowserWindow {

    public static void main(String[] args) {

        WebDriver driver = new ChromeDriver();

        driver.get("https://www.google.com");

        // Open a new browser window
        driver.switchTo().newWindow(WindowType.WINDOW);

        // Navigate to another website
        driver.get("https://www.facebook.com");

        System.out.println(driver.getTitle());

        driver.quit();
    }
}

4. Get All Window Handles After Opening a New Window

We can use getWindowHandles() to get the IDs of all opened browser windows and tabs.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WindowType;

import java.util.Set;

public class WindowHandlesExample {

    public static void main(String[] args) {

        WebDriver driver = new ChromeDriver();

        driver.get("https://www.google.com");

        // Store first window handle
        String parentWindow = driver.getWindowHandle();

        // Open a new tab
        driver.switchTo().newWindow(WindowType.TAB);

        driver.get("https://www.amazon.com");

        // Get all window handles
        Set<String> allWindows = driver.getWindowHandles();

        System.out.println("Total Windows: " + allWindows.size());

        for (String window : allWindows) {

            driver.switchTo().window(window);

            System.out.println("Window ID: " + window);
            System.out.println("Title: " + driver.getTitle());
        }

        // Switch back to parent window
        driver.switchTo().window(parentWindow);

        System.out.println("Parent Window: " + driver.getTitle());

        driver.quit();
    }
}

5. Difference Between Selenium 3 and Selenium 4

Selenium 3 Selenium 4
No direct newWindow() API Supports newWindow()
JavaScriptExecutor commonly used Native Selenium API available
Requires manual window switching Automatically switches to the newly created window
More code required Less code required

Selenium 3 approach:

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript("window.open('https://www.amazon.com');");

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

for (String window : windows) {
    driver.switchTo().window(window);
}

Selenium 4 approach:

driver.switchTo().newWindow(WindowType.TAB);

driver.get("https://www.amazon.com");

6. Difference Between getWindowHandle() and getWindowHandles()

Method Description Return Type
getWindowHandle() Returns the current browser window ID String
getWindowHandles() Returns IDs of all opened browser windows and tabs Set<String>

7. Real-Time Interview Questions

Q1. What is newWindow() in Selenium 4?

newWindow() is a Selenium 4 method used to open a new browser tab or window. It automatically switches the WebDriver focus to the newly opened context.

Q2. How do you open a new tab in Selenium 4?

driver.switchTo().newWindow(WindowType.TAB);

Q3. How do you open a new browser window in Selenium 4?

driver.switchTo().newWindow(WindowType.WINDOW);

Q4. Does Selenium automatically switch to the newly opened tab?

Yes. Selenium automatically switches the WebDriver focus to the newly created tab or window.

Q5. Can we open multiple tabs using newWindow()?

Yes.

driver.switchTo().newWindow(WindowType.TAB);
driver.get("https://www.google.com");

driver.switchTo().newWindow(WindowType.TAB);
driver.get("https://www.amazon.com");

driver.switchTo().newWindow(WindowType.TAB);
driver.get("https://www.facebook.com");

Q6. What is the difference between TAB and WINDOW?

  • TAB opens a new tab in the browser.
  • WINDOW opens a separate browser window.

8. Important Points to Remember

  • newWindow() was introduced in Selenium 4.
  • It belongs to the TargetLocator interface.
  • It supports WindowType.TAB and WindowType.WINDOW.
  • Selenium automatically switches to the newly created tab or window.
  • Use getWindowHandle() to get the current window ID.
  • Use getWindowHandles() to get all window IDs.
  • Use driver.close() to close the current window.
  • Use driver.quit() to close the entire browser session.
Interview Tip: Selenium 4's newWindow() method is particularly useful when automating workflows involving multiple tabs, payment gateways, external links, and authentication windows.

No comments:

Post a Comment