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.
1// Java23driver.get("https://www.selenium.dev/");4// A new window is opened and switches to it5driver.switchTo().newWindow(WindowType.WINDOW);6// Loads Sauce Labs open source website in the newly opened window7driver.get("https://opensource.saucelabs.com/");89// Python1011driver.get('https://www.selenium.dev/')12// A new window is opened and switches to it13driver.switch_to.new_window('window')14// Loads Sauce Labs open source website in the newly opened window15driver.get('https://opensource.saucelabs.com/')1617// C#1819driver.Navigate().GoToUrl(@"https://selenium.dev");20// A new window is opened and switches to it21driver.SwitchTo().NewWindow(WindowType.Window);22// Loads Sauce Labs open source website in the newly opened window23driver.Navigate().GoToUrl(@"https://opensource.saucelabs.com/");2425// Ruby2627driver.get 'https://selenium.dev'28// A new window is opened and switches to it29driver.manage.new_window(:window)30// Loads Sauce Labs open source website in the newly opened window31driver.get 'https://opensource.saucelabs.com/'3233// JavaScript3435await driver.get('https://selenium.dev');36// A new window is opened and switches to it37await driver.switchTo().newWindow('window');38// Loads Sauce Labs open source website in the newly opened window39await driver.get('https://opensource.saucelabs.com/');
Open a New Tab
1// Java23driver.get("https://www.selenium.dev/");4// A new tab is opened and switches to it5driver.switchTo().newWindow(WindowType.TAB);6// Loads Sauce Labs open source website in the newly opened window7driver.get("https://opensource.saucelabs.com/");89// Python1011driver.get('https://www.selenium.dev/')12// A new tab is opened and switches to it13driver.switch_to.new_window('tab')14// Loads Sauce Labs open source website in the newly opened window15driver.get('https://opensource.saucelabs.com/')1617// C#1819driver.Navigate().GoToUrl(@"https://selenium.dev");20// A new tab is opened and switches to it21driver.SwitchTo().NewWindow(WindowType.Tab);22// Loads Sauce Labs open source website in the newly opened window23driver.Navigate().GoToUrl(@"https://opensource.saucelabs.com/");2425// Ruby2627driver.get 'https://selenium.dev'28// A new tab is opened and switches to it29driver.manage.new_window(:tab)30// Loads Sauce Labs open source website in the newly opened window31driver.get 'https://opensource.saucelabs.com/'3233// JavaScript3435await driver.get('https://selenium.dev');36// A new tab is opened and switches to it37await driver.switchTo().newWindow('tab');38// Loads Sauce Labs open source website in the newly opened window39await driver.get('https://opensource.saucelabs.com/');40
Check out our comprehensive guide to Selenium 4 for more information.