Handle windows in webdriver

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 10


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.

How to handle windows in Selenium Webdriver

I have encountered with the below scenario https://ipay.adp.com/iPay/login.jsf Clicking on the login button in the page, I am getting pop up window which i am unable to handle. Could any one help me in resolving this scenario..

2 Answers 2

You can use autoit to automate the popup.

And then you can you can call the exe file using java

please perform following steps :
1. Download AutoIT tool. It can be downloaded from http://www.autoitscript.com/site/autoit/downloads/

Note: Please download AutoIT Full Installation

  1. While installing AutoIT, you will get two options: Run the Script and Edit the Script. If you are going to use the script without any modification, choose Run the Script option. If you want to customize the scripts, choose Edit the Script option (Recommended as we always needs to customize the scripts for our needs. Sometimes we need to create our own scripts.)
  2. Open the place where auto it is installed
  3. Open «Extras Folder» and then «Au3Record» For X-86 use this path C:\Program Files (x86)\AutoIt3\Extras\Au3Record
  4. Open Recorder wait for it to launch
  5. In browser perform steps to show that window
  6. Now Click Record button on Recorder
  7. Perform Key sequence by keyboard which you want to perform don’t use mouse just keyboard
  8. Now Click Stop
  9. Save you script and save it on some location
  10. Right Click on script and select compile(X86)

you will notice an .exe file created now use this .exe in your java code by following code

Process process=Runtime.getRuntime().exec(«abolute path of exe «); process.waitFor(); process.destroy(); or do following step open note pad and paste code mentioned below

region — Au3Recorder generated code Start (v3.3.9.5 KeyboardLayout=00000409) —

region — Internal functions Au3Recorder Start —

Func _Au3RecordSetup() Opt(‘WinWaitDelay’,100) Opt(‘WinDetectHiddenText’,1) Opt(‘MouseCoordMode’,0) Local $aResult = DllCall(‘User32.dll’, ‘int’, ‘GetKeyboardLayoutNameW’, ‘wstr’, ») If $aResult[1] <> ‘00000409’ Then MsgBox(64, ‘Warning’, ‘Recording has been done under a different Keyboard layout’ & @CRLF & ‘(00000409->’ & $aResult[1] & ‘)’) EndIf

endregion — Internal functions Au3Recorder End —

endregion — Au3Recorder generated code End —

save file as «authenticationAD.au3» now create exe of this file like the same steps mentioned above in steps 11-12 please change testUsername and password with correct ones but don’t remove and

if you are still facing this problem then tell me i will email you java file with auto it script

How to handle the new window in Selenium WebDriver using Java?

This is my code:

Now I have the next error:

Exception in thread «main» org.openqa.selenium.NoSuchElementException: Unable to find element with txtEnterCptCode (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 404 milliseconds.

6 Answers 6

It seems like you are not actually switching to any new window. You are supposed get the window handle of your original window, save that, then get the window handle of the new window and switch to that. Once you are done with the new window you need to close it, then switch back to the original window handle. See my sample below:

I have an utility method to switch to the required window as shown below

It will take you to desired window once title of the window is passed as parameter. In your case you can do.

and then again switch to parent window using the same method

Webdriver parentDriver = Utility.getHandleToWindow(«titleOfParentWindow»);

This method works effectively when dealing with multiple windows.

I have a sample program for this:

public class BrowserBackForward <

i was having some issues with windowhandle and tried this one. this one works good for me.

How to handle windows file upload using Selenium WebDriver?

I have seen lots of questions and solutions on File upload using Selenium WebDriver on Stack Overflow. But none of them are working for following scenario.

Someone has given a solution as following

But still I can’t find window handle. How can I work on that?

I am looking for a solution for the scenario above.

Please check this on any of the following websites.

15 Answers 15

Hey, that’s mine from somewhere :).

In case of the Zamzar web, it should work perfectly. You don’t click the element. You just type the path into it. To be concrete, this should be absolutely ok:

In the case of the Uploadify web, you’re in a pickle, since the upload thing is no input , but a Flash object. There’s no API for WebDriver that would allow you to work with browser dialogs (or Flash objects).

So after you click the Flash element, there’ll be a window popping up that you’ll have no control over. In the browsers and operating systems I know, you can pretty much assume that after the window has been opened, the cursor is in the File name input. Please, make sure this assumption is true in your case, too.

If not, you could try to jump to it by pressing Alt + N , at least on Windows.

If yes, you can «blindly» type the path into it using the Robot class. In your case, that would be something in the way of:

It sucks, but it should work. Note that you might need these: How can I make Robot type a `:`? and Convert String to KeyEvents (plus there is the new and shiny KeyEvent#getExtendedKeyCodeForChar() which does similar work, but is available only from JDK7).

For Flash, the only alternative I know (from this discussion) is to use the dark technique:

First, you modify the source code of you the flash application, exposing internal methods using the ActionScript’s ExternalInterface API. Once exposed, these methods will be callable by JavaScript in the browser.

Second, now that JavaScript can call internal methods in your flash app, you use WebDriver to make a JavaScript call in the web page, which will then call into your flash app.

How to handle multiple windows in Selenium?

While automating any website or a web application, we must have witnessed a scenario where multiple windows open within an application when a button is clicked and the user has to perform some action on all the opened windows. Now user might not be able to work on all windows at the same time and hence need some mechanism through which he can take control over parent and child windows or if I may say from a QA’s perspective, how can we handle window using Selenium?. In this tutorial, we will be understanding how this can be achieved with Selenium WebDriver. We will be focusing on the following key topics:

  • What is a window in Selenium?
    • How do we identify parent window and child windows?
  • Why do we need to handle multiple windows in Selenium?
  • What is a window handle in Selenium?
    • What are the different methods used for window handling in Selenium?
  • How do we handle child windows in Selenium?
    • How to handle multiple windows in Selenium?
    • Also, how do we switch back to the parent window from the child windows in Selenium?
    • How to close all windows in Selenium?

What is a window in Selenium?

A window in any browser is the main webpage on which the user is landed after hitting a link/URL. Such a window in Selenium is referred to as the parent window also known as the main window which opens when the Selenium WebDriver session is created and has all the focus of the WebDriver.

To view an example of how the main window looks like you can visit the ToolsQA demo site , and check. The same is shown in the screenshot below:

As you can see in the image above, even though the web page has multiple elements, but all of them are part of the main window. The URL navigated using the Selenium WebDriver will always have the context of the main window. But when we click on the buttons “New Window ” and “New Window Message “, it will open other windows, within the parent window. These windows are Child Windows. Let’s understand how to locate the parent and child windows?

How do we identify parent window and child windows?

When a user hits a URL, a webpage opens. This main page is the parent window i.e the main window on which the user has currently landed and will perform any operation. This is the same webpage that will open when our Selenium automation script will execute. All the windows which will open inside your main window will be termed as child windows.

Taking an example of ToolsQA demo site , the main page which has elements like the new tab, new window, etc is our parent window and the two sub-windows displayed are the child windows that will open when we click on “New Window” and “New Window Message” buttons, as shown below:

Note: There can be a single child window or multiple child windows in your parent window.

A child window may or may not have any URL. As shown above, Child Window 1 doesn’t have any explicit URL, whereas Child Window 2 has an explicit URL.

So, when we are testing a web application manually, it is very easy to check the behavior of child windows, as they are easily visible in the context of the main window. But the same is not the case while automating using Selenium. Let’s understand what is the need to handle the different window types when automating the application using Selenium WebDriver?

Why do we need to handle multiple windows in Selenium?

When a user is working on a web application, there might arise a scenario where a new window will open inside your main window. Consider a scenario of an e-commerce site selling garments that will have a size chart linked against each garment and upon clicking, a child window opens. Now, as we understand that Selenium works in a specific context only, and each of the child windows will have a separate context. So, for automating such scenarios using Selenium WebDriver, we have to direct the WebDriver accordingly, so as Selenium can get the context of the specific window and perform the needed actions in that window.

Let’s try to understand the concept of the different contexts for each window with the help of automating the scenario on – “https://demoqa.com/browser-windows”. After opening the URL, we will click on the “New window” button within the application, a new browser window opens. We will read the text from the newly opened window i.e ” This is sample page” and will print it.

The below code-snippet will click on the “New Window ” button and will try to read the text from the new Window.

Читайте также:  Узнать количество оперативной памяти линукс
Оцените статью