Selenium 4 - New Windows and Tab Utilities

Selenium 4 includes a new command to help users create a new tab or a new window. This article provides some code examples that show how to use it.

It might be necessary to open a new tab or a new window in a given test scenario. Before, it was common to see automation scripts sending a combination of keys such as “Ctrl” + “T” to open a new tab, which usually led to different results depending on the browser.

Selenium 4 includes the new newWindow command to help users create a new tab or a new window. After the command is used, the focus will be on the created tab or window. Here are some code examples showing how to use it. For links to these examples in our demo repos, look at our Selenium 4 Documentation.

Java

driver.get("https://www.selenium.dev/");
// A new window is opened and switches to it
driver.switchTo().newWindow(WindowType.WINDOW);
// Loads Sauce Labs open source website in the newly opened window
driver.get("https://opensource.saucelabs.com/");

Python

driver.get('https://www.selenium.dev/')
// A new window is opened and switches to it
driver.switch_to.new_window('window')
// Loads Sauce Labs open source website in the newly opened window
driver.get('https://opensource.saucelabs.com/')

C#

driver.Navigate().GoToUrl(@"https://selenium.dev");
// A new window is opened and switches to it
driver.SwitchTo().NewWindow(WindowType.Window);
// Loads Sauce Labs open source website in the newly opened window
driver.Navigate().GoToUrl(@"https://opensource.saucelabs.com/");

Ruby

driver.get 'https://selenium.dev'
// A new window is opened and switches to it
driver.manage.new_window(:window)
// Loads Sauce Labs open source website in the newly opened window
driver.get 'https://opensource.saucelabs.com/'

JavaScript

await driver.get('https://selenium.dev');
// A new window is opened and switches to it
await driver.switchTo().newWindow('window');
// Loads Sauce Labs open source website in the newly opened window
await driver.get('https://opensource.saucelabs.com/');

Open a New Tab

Java

driver.get("https://www.selenium.dev/");
// A new tab is opened and switches to it
driver.switchTo().newWindow(WindowType.TAB);
// Loads Sauce Labs open source website in the newly opened window
driver.get("https://opensource.saucelabs.com/");

Python

driver.get('https://www.selenium.dev/')
// A new tab is opened and switches to it
driver.switch_to.new_window('tab')
// Loads Sauce Labs open source website in the newly opened window
driver.get('https://opensource.saucelabs.com/')

C#

driver.Navigate().GoToUrl(@"https://selenium.dev");
// A new tab is opened and switches to it
driver.SwitchTo().NewWindow(WindowType.Tab);
// Loads Sauce Labs open source website in the newly opened window
driver.Navigate().GoToUrl(@"https://opensource.saucelabs.com/");

Ruby

driver.get 'https://selenium.dev'
// A new tab is opened and switches to it
driver.manage.new_window(:tab)
// Loads Sauce Labs open source website in the newly opened window
driver.get 'https://opensource.saucelabs.com/'

JavaScript

await driver.get('https://selenium.dev');
// A new tab is opened and switches to it
await driver.switchTo().newWindow('tab');
// Loads Sauce Labs open source website in the newly opened window
await driver.get('https://opensource.saucelabs.com/');

Check out our comprehensive guide to Selenium 4 for more information.


Written by

Diego Molina and Titus Fortner


Topics

SeleniumAutomated testing