About this lesson
Selenium Python switch_to.new_window: open a tab, work in it, close it and return
Selenium Python AssignmentsAdvancedWindow Management
How it works
Selenium 4 added driver.switch_to.new_window. Pass it the word "tab" and the browser opens a fresh tab and moves the driver there in one step. It returns nothing useful, so ask for current_window_handle afterwards to learn the new handle.
The new tab starts empty. Right after creation the title was an empty string and the address was about:blank. Nothing loads until you call get() there, which is why you load a page before reading the title.
driver.close() is where people slip. It closes the window the driver points at now, not the one you have in mind. Afterwards the driver still points at the dead window, and even driver.title raises NoSuchWindowException with "target window already closed". Call switch_to.window with a surviving handle before anything else. It wants one string. A whole list of handles raises InvalidArgumentException.
About the assignment
Start on Text Fields, open Buttons in a second tab, hop between the two, remove the second tab, and confirm one window is left. Both handles must be saved.
What you will do
- Save the first window's handle before opening anything.
- Open a tab, load Buttons in it, print the window count and the title, and save this tab's handle too.
- Switch to the saved first handle and print its title.
- Switch to the new tab, close it, switch back to the first window, and print the remaining count as text.
Common mistakes
Closing before switching to the tab
After switching back, the driver sits on the original window.
close()there removes the original and leaves the tab you meant to remove.Using the driver right after close()
Any call on the closed window fails with
NoSuchWindowException: no such window: target window already closeduntil you switch.Passing the wrong thing to switch_to.window
A list of handles or an index number gives
InvalidArgumentException: invalid argument: 'handle' must be a string. Pass one handle.
In a real test suite
Suites often keep the original handle in a fixture, run the test, and switch back in teardown. Handles live in variables, never as typed literals, since they change each run.
Window size also keeps runs stable, and lesson 55 sets it.
Related lessons
- Read the Open WindowIntroduces handles and the window list this lesson switches between.
- Set the Window SizeControls the size of the window you are in.
- Visit Two Pages with driver.get()Loads two pages with
get(), which you do again inside the new tab.
To try one of these calls on any page without a task, open the Online Selenium Python Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
from selenium import webdriver
driver = webdriver.Chrome()
try:
driver.get("https://testkru.com/Elements/TextFields")
# Open https://testkru.com/Elements/Buttons in a new tab, return, then close the new tab
finally:
driver.quit()