python-3.xseleniumselenium-webdriverwindow-handleswebdriver-w3c-spec

How to convert windows IDs to human readable format using Selenium and Python


I know that if I want to know the corresponding IDs of the windows I currently have open, I use the following sentence on Python:

current_windows = driver.window_handles
print(current_windows)

Output (suppose there were 2 windows opened):

['CDwindow-807A80F3D56E82E4A61529E5898AC71C', 'CDwindow-7CEAB7D7E9B701F6279C4B5C4AEE1A29']

And I also know that if I want to get the title of the current page in the driver, I use the following sentence on Python:

current_window = driver.title
print(current_window)

Output:

Google

However, these windows IDs can't be understood by a mortal like me, so how could improve the sentence above to get the title of those windows?

I mean, to get an output like this that contains all the titles of the current windows open in the driver:

['Google', 'Facebook']

Solution

  • As per the WebDriver Specification of Window Handle:

    getwindowhandle

    where,

    Each browsing context has an associated window handle which uniquely identifies it. This must be a String and must not be "current".

    The web window identifier is the string constant "window-fcc6-11e5-b4f8-330a88ab9d7f".

    So the output of the following command:

    current_windows = driver.window_handles
    print(current_windows)
    

    as:

    ['CDwindow-807A80F3D56E82E4A61529E5898AC71C', 'CDwindow-7CEAB7D7E9B701F6279C4B5C4AEE1A29']
    

    is as per the specification and neither contains the page title information nor can be converted to any human readable format.

    To retrieve the page title the only command is:

    print(driver.title)