Skip to content
CodeKruPro

Read the Open Window

53/80

Read the Open Window

current_window_handle identifies the window you are in. window_handles is the list of open windows. Print that a handle exists and how many windows are open. Do not print the handle string.

Task:

  1. Open Buttons
  2. Print whether a handle exists
  3. Print the window count and the title

Print the lines below exactly.

Expected output:

Handle exists: True
Window count: 1
Title: Buttons

About this lesson

Selenium Python current_window_handle and window_handles: count the open windows

Selenium Python AssignmentsAdvancedWindow Management

How it works

Every window or tab Selenium controls has a handle, a long string that identifies it. The driver always points at one window. driver.current_window_handle returns that window's handle. driver.window_handles returns a list of every open handle, in the order the windows opened.

Both are properties, not methods. Parentheses after either one fail with TypeError, and the message says the str or list object is not callable. People coming from Java trip on this, because there they are method calls.

The handle string means nothing to you. It is 32 random characters and differs on every run, so tests never compare it with a fixed value. What you can check is that it exists, that it appears in the list, and how long the list is. len() gives the count. Python will not join a number or a boolean to text with +, so convert with str() before printing.

About the assignment

Open the Buttons page and report three facts: whether a current handle exists, how many windows are open, and the page title. The handle itself must stay out of your output.

What you will do

  1. Load the page, then read the current handle into a variable.
  2. Turn the handle into a yes or no answer with bool, and convert that answer to text so it can follow a label.
  3. Take the length of the window list and convert it to text too.
  4. Read the title as a property, then print the three labelled lines in the order the task gives.

Common mistakes

  • Calling a property like a function

    driver.window_handles() raises TypeError: 'list' object is not callable. The handle version says 'str' object is not callable. Drop the parentheses.

  • Joining an int or bool to text

    Gluing a count onto a label with + raises a TypeError, and a boolean does the same. Convert with str() first.

  • Printing the handle

    Any line containing it changes on every run. Print what the handle tells you, not the handle.

In a real test suite

Real suites use window counts as a guard. A click that should open a tab is followed by a wait on the length of the handle list, and only then does the test switch. A small helper in a page object keeps that wait out of the test body.

The next lesson makes a second window and uses a saved handle to find the way back.

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/Buttons")
    # Print that a handle exists, the count, and the title
finally:
    driver.quit()