Back to Resources

Blog

Posted October 20, 2021

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.

quote

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
// Java
2
3
driver.get("https://www.selenium.dev/");
4
// A new window is opened and switches to it
5
driver.switchTo().newWindow(WindowType.WINDOW);
6
// Loads Sauce Labs open source website in the newly opened window
7
driver.get("https://opensource.saucelabs.com/");
8
9
// Python
10
11
driver.get('https://www.selenium.dev/')
12
// A new window is opened and switches to it
13
driver.switch_to.new_window('window')
14
// Loads Sauce Labs open source website in the newly opened window
15
driver.get('https://opensource.saucelabs.com/')
16
17
// C#
18
19
driver.Navigate().GoToUrl(@"https://selenium.dev");
20
// A new window is opened and switches to it
21
driver.SwitchTo().NewWindow(WindowType.Window);
22
// Loads Sauce Labs open source website in the newly opened window
23
driver.Navigate().GoToUrl(@"https://opensource.saucelabs.com/");
24
25
// Ruby
26
27
driver.get 'https://selenium.dev'
28
// A new window is opened and switches to it
29
driver.manage.new_window(:window)
30
// Loads Sauce Labs open source website in the newly opened window
31
driver.get 'https://opensource.saucelabs.com/'
32
33
// JavaScript
34
35
await driver.get('https://selenium.dev');
36
// A new window is opened and switches to it
37
await driver.switchTo().newWindow('window');
38
// Loads Sauce Labs open source website in the newly opened window
39
await driver.get('https://opensource.saucelabs.com/');

Open a New Tab

1
// Java
2
3
driver.get("https://www.selenium.dev/");
4
// A new tab is opened and switches to it
5
driver.switchTo().newWindow(WindowType.TAB);
6
// Loads Sauce Labs open source website in the newly opened window
7
driver.get("https://opensource.saucelabs.com/");
8
9
// Python
10
11
driver.get('https://www.selenium.dev/')
12
// A new tab is opened and switches to it
13
driver.switch_to.new_window('tab')
14
// Loads Sauce Labs open source website in the newly opened window
15
driver.get('https://opensource.saucelabs.com/')
16
17
// C#
18
19
driver.Navigate().GoToUrl(@"https://selenium.dev");
20
// A new tab is opened and switches to it
21
driver.SwitchTo().NewWindow(WindowType.Tab);
22
// Loads Sauce Labs open source website in the newly opened window
23
driver.Navigate().GoToUrl(@"https://opensource.saucelabs.com/");
24
25
// Ruby
26
27
driver.get 'https://selenium.dev'
28
// A new tab is opened and switches to it
29
driver.manage.new_window(:tab)
30
// Loads Sauce Labs open source website in the newly opened window
31
driver.get 'https://opensource.saucelabs.com/'
32
33
// JavaScript
34
35
await driver.get('https://selenium.dev');
36
// A new tab is opened and switches to it
37
await driver.switchTo().newWindow('tab');
38
// Loads Sauce Labs open source website in the newly opened window
39
await driver.get('https://opensource.saucelabs.com/');
40

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

Titus Fortner
Sr. Developer Experience Engineer, Sauce Labs
Diego Molina
Staff Software Engineer at Sauce Labs
Published:
Oct 20, 2021
Share this post
Copy Share Link
© 2023 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered trademarks owned by Sauce Labs Inc. in the United States, EU, and may be registered in other jurisdictions.