Open new tab and window in Chrome browser using Selenium 4 and JAVA

Many times we thought why selenium doesn’t have options to open new tab or window through code. Now, the wait is over. This feature is added in Selenium 4. Lets see how to work on it!!

Versions used while doing this blog post, (These are the prerequisites to automate chrome browser. Browser and driver versions should match)
selenium-java version – 4.4.0
chrome browser version – 105
chrome driver version – 105 (Download link : https://chromedriver.chromium.org/downloads) [Used WebDriverManager to auto download the expected driver version]

Chrome browser can be opened using selenium by following below steps:
1. Map the chromedriver using WebDriverManager,

WebDriverManager.chromedriver().setup();

2. Instantiate (creation of object) driver object with ChromeDriver() class (This step opens the chrome browser)

WebDriver driver = new ChromeDriver();

3. Now, next steps of maximizing the browser, setting implicit timeout for driver object and launch of expected URL can be carried out in default window

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
driver.manage().window().maximize();
driver.get("https://www.google.com");
System.out.println("Page title -> "+driver.getTitle());

4. Now, lets open a new tab and open another URL in it

driver.switchTo().newWindow(WindowType.TAB);
driver.get("https://crazyautomator.com/find-elements-with-relative-locator-strategy-using-selenium-4-and-java/");
System.out.println("Page title of new tab -> "+driver.getTitle());

5. Now, lets open a new window and open another URL in it

driver.switchTo().newWindow(WindowType.WINDOW);
driver.get("https://crazyautomator.com/read-server-client-logcat-logs-using-appium-and-java/");
System.out.println("Page title of new window -> "+driver.getTitle());

6. Quit/close the driver object after automating the necessary flows within the website.

driver.quit();

Sample code for reference

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;

public class OpenNewTabAndWindowInChromeUsingSelenium4 {

    public static void main(String[] args){
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
        driver.manage().window().maximize();
        driver.get("https://www.google.com");
        System.out.println("Page title -> "+driver.getTitle());

        driver.switchTo().newWindow(WindowType.TAB);
        driver.get("https://crazyautomator.com/find-elements-with-relative-locator-strategy-using-selenium-4-and-java/");
        System.out.println("Page title of new tab -> "+driver.getTitle());

        driver.switchTo().newWindow(WindowType.WINDOW);
        driver.get("https://crazyautomator.com/read-server-client-logcat-logs-using-appium-and-java/");
        System.out.println("Page title of new window -> "+driver.getTitle());

        driver.quit();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *