Handling multiple windows in Python Selenium

The Blog To Learn Selenium and Test Automation

Handling multiple windows in Python Selenium

There are many cases where handling multiple windows while working with a web application is required, either application navigates to or opens multiple windows and user has to perform operations in this new window. Those are may be advertisements or kind of information showing on popup windows such as terms & conditions, privacy policy or kind of web page itself where user has to enter information.

Python Selenium provides option to handle multiple windows using ‘window_handles’. Python Selenium WebDriver assigns an id to each window as soon as the WebDriver object is instantiated or new window is opened using a WebDriver object. This unique id is known as window handles.

Also note that WebDriver object always controls only one window at a time in the current session. For example, opening a link in new window does not transfer control of WebDriver to new window. WebDriver will be still controlling the old window and any operations that we perform using Selenium script will be forwarded to this old window.

We can use this unique id to differentiate a window and switch control among multiple windows.

Python provides two in-built objects

window_handles

Returns the handles of all windows within the current session.

        Syntax: driver.window_handles

current_window_handle

Returns the handle of the current window.

        Syntax: driver.current_window_handle

Example 1:
For example, to print title of all windows in the current session

To print title of multiple windows opened, we can follow below steps.
1. Get all window handles
2. Switch to the window using driver.switch_to.window(handles)
3. Get and print window title

Assume that driver.find_element_by_id(“link”).click(); will open up a new window in current session.

from selenium import webdriver

driver = webdriver.Firefox(executable_path="/data/Softwares/BrowserDrivers/geckodriver")
driver.get("file:///data/WorkArea/Python_Test.html")

driver.find_element_by_id("link").click()

handles = driver.window_handles
size = len(handles)

for x in range(size):
  driver.switch_to.window(handles[x])
  print(driver.title)

Example 2:
For example, User wants to print title of all windows except current window.

We can follow below steps to print title of new window among multiple windows.

1. Get all window handles
2. Get current window handle
3. If handle is not current window handle, Switch to the window using driver.switch_to.window(handles)
4. Get and print window title

Assume that driver.find_element_by_id(“link”).click(); will open up a new window in current session.

from selenium import webdriver

driver = webdriver.Firefox(executable_path="/data/Softwares/BrowserDrivers/geckodriver")
driver.get("file:///data/WorkArea/Python_Test.html")

driver.find_element_by_id("link").click()

handles = driver.window_handles
size = len(handles)
for x in range(size):
  if handles[x] != driver.current_window_handle:
    driver.switch_to.window(handles[x])
    print(driver.title)

Example 3:
For example, User wants to do some operation in newly opened child window, close it after all operations and do some actions in parent window.

We can follow below steps to perform this multiple windows operation.

1. Get all window handles
2. Get parent window handle and store in a temp variable say ‘parent_handle’
3. If handle is not parent window handle, Switch to the child or new window using driver.switch_to.window(handles)
4. Perform all required operations and close the child or new window
5. Shift the control back to parent window
6. Perform required operations

Assume that driver.find_element_by_id(“link”).click(); will open up a new window in current session.

from selenium import webdriver

driver = webdriver.Firefox(executable_path="/data/Softwares/BrowserDrivers/geckodriver")
driver.get("file:///data/WorkArea/Python_Test.html")

driver.find_element_by_id("link").click()

handles = driver.window_handles
size = len(handles)
parent_handle = driver.current_window_handle
for x in range(size):
  if handles[x] != parent_handle:
    driver.switch_to.window(handles[x])
    print(driver.title)
    driver.close()
    break

driver.switch_to.window(parent_handle)

driver.find_element_by_id("link").click()

Let’s see in another post, how to handle new/multiple tabs in same browser window.

Happy coding!!! Please let us know your thoughts in comments section.

 

13 Responses

  1. Gema Spradlin says:

    This is such a great post, and was thinking much the same myself. Be sure to keep writing more great articles like this one.

  2. Gaye Mammenga says:

    I just put the link of your blog on my Facebook Wall. very nice blog indeed..”..:

  3. Jayashree says:

    Can you provide me sample website for selenium practice

  4. Anshul says:

    Thank you ! Easy to understand. Precise. Great job.

  5. Veronique Deville says:

    Wohh precisely what I was looking for, appreciate it for posting.

  6. Kevin Vignola says:

    Thanks for sharing such u\a useful information.

  7. Keesha Hinson says:

    I am very happy to read this. This is the type of manual that needs to be given and not the accidental misinformation that is at the other blogs.
    Appreciate your sharing this best doc.

  8. Georgene Eldert says:

    Thanks for sharing such u\a useful information.

  9. Sami says:

    Thank you
    I wish the source code was more readable. The gray level is like white, so I need to copy the source code to notepad in order to read it

  10. Derek Gamon says:

    There are some interesting points in time in this article however I don’t know if I see all of them heart to heart. There’s some validity but I will take maintain opinion until I look into it further. Good article , thanks and we would like more! Added to FeedBurner as properly

Leave a Reply

Your email address will not be published. Required fields are marked *