How to open a new tab using Selenium WebDriver in Java?

50    Asked by Aalapprabhakaran in QA Testing , Asked on Dec 5, 2024

I would like to instruct an already running session of the Firefox browser to open a new tab with the help of Selenium web driver (previously known as Selenium 2) in Java. What are the best ways to do this?

Answered by Brian Kennedy

To open a new tab in the existing Firefox browser using Selenium WebDriver in Java, you can use the following approaches:

1. Using JavaScript Execution

The most common method to open a new tab is by using JavaScript. Selenium WebDriver's JavascriptExecutor allows you to execute JavaScript code.

Here’s the example:

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;


public class OpenNewTab {

    public static void main(String[] args) {

        // Set the path to the GeckoDriver

        System.setProperty("webdriver.gecko.driver", "path/to/geckodriver");


        // Initialize the WebDriver

        WebDriver driver = new FirefoxDriver();


        // Open the first URL

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


        // Cast WebDriver to JavascriptExecutor

        JavascriptExecutor js = (JavascriptExecutor) driver;


        // Open a new tab

        js.executeScript("window.open();");


        // Switch to the new tab

        for (String handle : driver.getWindowHandles()) {

            driver.switchTo().window(handle);

        }


        // Load a different URL in the new tab

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


        // Clean up

        driver.quit();

    }

}


2. Using Keyboard Shortcuts

You can use the sendKeys() method to simulate keyboard shortcuts (e.g., Ctrl + t) for opening a new tab.

Here’s an example:

import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.interactions.Actions;


public class OpenNewTabWithKeys {

    public static void main(String[] args) {

        // Set the path to the GeckoDriver

        System.setProperty("webdriver.gecko.driver", "path/to/geckodriver");


        // Initialize the WebDriver

        WebDriver driver = new FirefoxDriver();


        // Open the first URL

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


        // Create an Actions instance

        Actions actions = new Actions(driver);


        // Press Ctrl + t (or Command + t on Mac)

        actions.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).perform();


        // Switch to the new tab

        for (String handle : driver.getWindowHandles()) {

            driver.switchTo().window(handle);

        }


        // Load a different URL in the new tab

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


        // Clean up

        driver.quit();

    }

}

Explanation

  JavascriptExecutor Method:

It uses JavaScript to create a new tab.

Then, you switch to the new tab using driver.getWindowHandles().

Keyboard Shortcuts:

Simulates Ctrl + t to open a new tab in the browser.

Both methods work well with modern Selenium versions, but the JavaScript approach is often more robust.



Your Answer

Interviews

Parent Categories