javaseleniumselenium-webdriverwindow-handlesselenium4

How to open a new window using the new method WindowType of Selenium 4


The release notes of Selenium v4.0.0.0-alpha-1 mentions about:

* Added command to open a new window.

Source code:

  public static WindowType fromString(String text) {
    if (text != null) {
      for (WindowType b : WindowType.values()) {
        if (text.equalsIgnoreCase(b.text)) {
          return b;
        }
      }
    }
    return null;
  }
}

Can someone help me to open a tab/window using the new method WindowType of Selenium v4.x?


Solution

  • Method newWindow(WindowType typeHint) is introduced to open the new window or tab in Selenium 4

    /**
     * Creates a new browser window and switches the focus for future commands of this driver
     * to the new window.
     * <p>
     * See <a href="https://w3c.github.io/webdriver/#new-window">W3C WebDriver specification</a>
     * for more details.
     *
     * @param typeHint The type of new browser window to be created. The created window is not
     *                 guaranteed to be of the requested type; if the driver does not support
     *                 the requested type, a new browser window will be created of whatever type
     *                 the driver does support.
     * @return This driver focused on the given window
     */
    WebDriver newWindow(WindowType typeHint);
    

    Sample Usage

    System.setProperty("webdriver.edge.driver","D:\\SomeLocation\\msedgedriver.exe");
    WebDriver  driver = new EdgeDriver();
    driver.get("https://www.google.com");
    
    // This will open the new tab
    driver.switchTo().newWindow(WindowType.TAB);
    driver.get("http://www.google.com");
    
    // This will open the new window
    driver.switchTo().newWindow(WindowType.WINDOW);
    driver.get("http://www.google.com");