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
Hmm is anyone else encountering problems with the pictures on this blog loading? I’m trying to figure out if its a problem on my end or if it’s the blog. Any responses would be greatly appreciated.
Thanks for sharing, this is a fantastic article. Will read on…