Windows handle in selenium

How to handle multiple windows in Selenium?

By Neha Vaidya, Community Contributor — June 26, 2020

Software testing plays a vital role in creating error-free products that offer seamless user experiences. Therefore, it is essential to test the software before putting it into production.

As technology advances, it is necessary to adopt automation testing into the development pipeline. Selenium is the most widely used framework for automated software testing of a website. Every website must be tested by putting it through multiple real-world user scenarios. One such scenario is the handling of multiple windows. This article will discuss how to automate this action with Selenium so that the website’s behavior can be monitored by testers.

Handling Multiple Windows in Selenium

The user scenario being automated here is: Open a new tab and then switch back to the last window to complete the other pending activities. In such scenarios, Selenium helps to handle multiple windows through window handlers and javascript executors.

What is a window handle?

It is a unique identifier that holds the address of all the windows. Think of it as a pointer to a window, which returns the string value. It is assumed that each browser will have a unique window handle. This window handle function helps to retrieve the handles of all windows.

Syntax

  1. get.windowhandle(): This method helps to get the window handle of the current window
  2. get.windowhandles(): This method helps to get the handles of all the windows opened
  3. set: This method helps to set the window handles in the form of a string. set set= driver.get.windowhandles()
  4. switch to: This method helps to switch between the windows
  5. action: This method helps to perform certain actions on the windows

These are some of the methods that will be used to handle multiple windows in Selenium.

Example of handling multiple windows

Scenario: Navigate to the Browserstack home page. This is the parent window. From the parent window, let’s see how to handle the child windows and then again navigate back to the parent windows.

Steps to execute:

  1. Get the handle of the parent window using the command: String parentWindowHandle = driver.getWindowHandle();
  2. Print the window handle of the parent window.
  3. Find the element on the web page using an ID which is an element locator.
  4. Open multiple child windows.
  5. Iterate through child windows.
  6. Get the handles of all the windows that are currently open using the command: Set allWindowHandles = driver.getWindowHandles(); which returns the set of handles.
  7. Use the SwitchTo command to switch to the desired window and also pass the URL of the web page.

Refer to the complete program below.

Before running the code, one should do a quick check on 6 things to avoid while running selenium scrips. Check it out.

Output:

On executing the parent window handle, it will open multiple child windows and then navigate back to the final window handle.

Now let’s perform some actions on the Browserstack website.

  • Use the javascriptexecutor to scroll down through a page.
  • Find the element using XPath and send keys (which is of the form string) to that particular element location.
  • Declare the web element Link to click on a particular link on the page. In this case, the link must open in a new window.
  • Get the window handles of all the windows and print them in a sequential manner.
  • Switch to the parent window and check if the title matches. If it does, scroll down the page using the javascriptexecutor.
  • Find another element on the web page using the element locator and specify the position of the new window.
  • Switch back to the parent window and scroll down through the page.
Читайте также:  Удалить лишнюю копию windows


Code Snippet

On executing the code above, it will launch multiple windows and the window handle will be retrieved. Run the code, automate user navigation through multiple windows, and ensure the website works perfectly in real user conditions. With Selenium WebDriver, ensure that websites offer an optimal user experience in all possible circumstances.

Multiple Windows Handling in Selenium

Multiple Windows in Selenium

There will be times when you will require to perform some testing, where the testing operations open a new browser/tab; a test case may required you to perform some tasks on the newly opened browser window/tab and return back to the original window to perform the remaining tasks.

Even if the window/tab is currently on focus, but still, it is not an active window, so to perform some tasks, you need to switch to a new browser window/tab in webdriver.

Commands will have an effect on the base window unless we switch our control to a new window/tab.

Previously tab and Windows are two different items, as tabs don’t have any GU iD. Now developers made tab and windows have GUIDs, so tabs and windows are treated as same in selenium.

Situations when we are likely to deal with multiple windows:

  • Filling forms may require selecting the date from a separately opened window
  • Clicking on some link/button can open another window
  • Handling Advertisement windows

Multiple Windows

Above image depicts multiple browser windows

  • Our Application is opened on Parent/Base window, the total number of windows is one
  • When clicking on a link/button our application opens two new windows along with base window, now the total number of windows present is Three (base + child-1 + child-2)
  • Now move to Child-1 window and perform click on a button/link which opens another one more window along with child-window, now the total number of windows is 4 (base + child-1 + child-2 + grand child-1)
  • We can perform an operation on a grandchild -1 window, and we can close all windows or specific windows.

If we perform the above operation manually, we may not find difficulty, but handling the above scenario is tricky in selenium.

Get Window Handles in Selenium

Selenium provides few methods to handle the multiple windows; let’s see what the methods and their uses are.

GU ID:
GU ID abbreviation of Globally Unique Identifier, Every OS generates GU ID for application to identifying them uniquely. We will be using this GU ID to handle the multiple browsers; GU ID is a numeric string value.

getWindowHandle() :
getWindowHandle method in webdriver returns the current(active) browser’s GU ID. It return GU ID as string value.

getWindowHandle() :
getWindowHandles method in selenium returns GU ID of all the browsers present at the moment, which are opened by the current driver. This method returns GU IDs as Set of String .

Set Collection in Java : Set is one of the Collection types, and it stores the values at random places in memory. We cannot access the values based on the index as Set stores values in random places ( this is one of the significant differences between List and Set ). You may get wrong values if you try to access values based on the index.

switchTo().window() : switchTo() method in webdriver helps user to switch between windows, frames, elements, alerts. switchTo().window(GU ID) method switches the control from the current browser window to the target browser window, which has the specified «GU ID».

Handle simple Two Browser Windows / Tabs

Click the button to open a new window ( google.com)

Читайте также:  Синтаксис команд командной строки windows

Please use Firefox as a browser to open the link in a new window; if you use chrome, the link may be opened in a new tab rather than a new window.

Steps to Handle Two Windows :
1. Open Firefox browser and Navigate to https://chercher.tech/python/windows-selenium-python

2. Get the GU ID of the current (parent) window using getWindowHandle() method present in the webdriver and store the value in a String

3. Click on the Open New Window button, application open new window with google page. Make the webdriver to sleep for 5 seconds; otherwise, it may not find the newly opened tab.

4. Get the GU IDs of the two windows (parent + google), using the getWindowHandles() method present in the webdriver. Store the GU IDs in a Set Collection; this Set will have GU IDs of both parent and Child Browsers

5. iterate the Set of GUID values, and if the value is parent value skip it if not switch to the new window

6. Switch to the window using switchTo().window() method, pass the GU ID of the child browser to this method.

7. Find the search bar in Google.com and search for «success»

Close the Google tab/Window and return to parent tab/browser window

Complete code for switching window may look like below

More than Two Windows handling

We have handled the two windows in the above tutorial by comparing GUID, but when we have more than two windows, we cannot use the same approach. To access the exact page, we may need the title of the page, based on the title of the page we can differentiate the browser windows in selenium.

Tip : We can use the title, URL, element reference while switching into a new window to ensure that we are switching to the required window. I prefer Tile, URL than the element.

Scenario : Click on the Open 3 New Windows button, it will open three more windows (bing, google, yahoo), now switch to bing and search for «gates».

Steps to Handle More than Two Windows :
1. Open Firefox browser and Navigate to https://chercher.tech/python/windows-selenium-python

2. Get the GU ID of the current (parent) window using getWindowHandle() method present in the selenium and store the value in a String

3. Click on the Open 3 New Window button, application open new window with google page. Make the webdriver to sleep for 5 seconds; otherwise, it may not find the newly opened tab.

4. Get the GU IDs of the two windows (parent + google + bing + yahoo), using getWindowHandles()method. Store the GU IDs in a Set Collection; this Set will have GU IDs of both parent and Child Browsers

5. Now iterate through the Set, switch to the first GUID in Set and check the page title contains the keyword «bing».

Complete program for switching multiple windows in selenium

A developer can make the link to open in a new window using the target keyword present in anchor tag when the developer sets this value to «_blank»; the link will be opened in new windows when the user clicks the link.

If the developer avoids using target=’_blank’, the browser opens such links in the same window. It is recommended not to open the link in a new window.

Force to open in new Window rather than Tab

There will be situations where we have to open a link in a new window, but later browser does not support opening a URL in a new window as it is not recommended ( phishing is easy with new windows ).

During such scenarios, we can use action class to open the link in a new window by pressing the Shift key, and to click the link; it opens the link in a new window in the webdriver.

Читайте также:  Как добавить сканер kyocera windows 10

Steps to open link in a new Window :
1. Open Firefox browser and Navigate to https://chercher.tech/python/windows-selenium-python

2. Find the above link using id force-new-window and store it in a variable of WebElement type

3. Create an object for Actions class

4. Call the keyDown method from the Actions class object and pass Keys.SHIFT as parameter

5. Call Click method from the Actions class object, and pass the stored web element as a parameter to this method

6. Use build() method from Actions class object to bind the keyDown and click methods, call the perform method to perform the operations.

Complete program for opening a link in a new window in selenium.

Recommended Readings

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

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.

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.

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.

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.

Оцените статью