Skip to content
CodeKruPro

Open a Tab and Return to the Original Window

54/80

Open a Tab and Return to the Original Window

Save the current handle, open a tab with switch_to.new_window("tab"), and switch back with switch_to.window. Do not print handle strings.

Task:

  1. Start on Text Fields and save the handle
  2. Open a tab to Buttons and print the count and the new title
  3. Switch back and print the original title
  4. Switch to the new tab, close it, switch back, and print how many windows remain

Print the lines below exactly.

Expected output:

Windows open: 2
New tab title: Buttons
Original title: Text Fields
After close: 1

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

  1. Save the first window's handle before opening anything.
  2. Open a tab, load Buttons in it, print the window count and the title, and save this tab's handle too.
  3. Switch to the saved first handle and print its title.
  4. 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 closed until 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.

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.

main.py
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()