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
- Load the page, then read the current handle into a variable.
- Turn the handle into a yes or no answer with
bool, and convert that answer to text so it can follow a label. - Take the length of the window list and convert it to text too.
- 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()raisesTypeError: '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 aTypeError, and a boolean does the same. Convert withstr()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.
Related lessons
- Open Chrome and Read the Page TitleReads the page title as a property, the same style used here.
- Open a Tab and Return to the Original WindowOpens a second tab and switches between handles, so the list grows to two.
- Count Employee Rows and Columns with len()Uses
len()on a list of elements, as this lesson does on a list of handles.
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/Buttons")
# Print that a handle exists, the count, and the title
finally:
driver.quit()