WebDriver Tutorial 4 : Better Window Handling

This is Part 4 of the Selenium WebDriver Tutorial Series. Check out Part 3 to Master WebDriver API.

Window handling is one of the most frustrating areas in webdriver if not handled properly. Automation tester often face the problem of their script not being on the correct window, not making the proper window switches, closing of windows etc.

We will try to get some core concepts clear here so that you can avoid these issues or same time in resolving them.

Summary

 

Target attribute of Link

Hyperlinks with target=_blank values opens the link in a new window (or tab), So in case you have to find out whether a link opens in the same window or new window, checking the value for target attribute of the hyperlink will help.

Current Window Handling

We can use the getWindowHandle method to get the current window handle. It returns a string which uniquely identifies the current window.

We can use getWindowHandles to get all the currently opened windows as a set of strings. Please note that this will only return the windows that was opened by this webdriver instance. The windows opened manually or by another webdriver instance will not be returned. The second part is important because, it allows us to run multiple webdriver instances parallelly without interference.

We can loop through the list of all windows to get windows other than the current one.

SEE ALSO:   Setting up WebDriver with Maven

Switching between windows

Switching between windows is done using the handle obtained by either getWindowHandle or getWindowHandles methods.

Normally, before we click on a link which opens a new window(target=_blank), we should get the current window handle using driver.getWindowHandle(). Then we get all the window handles and switch to the other window. Below code switches to the window other than the current window. Note that if more than 2 windows are present, it switches to the second window.

As you can see, for switching to a new window, we have to provide the handle or name of the window to switch to.

If we know the name of the window, we can use it directly without having to take all the window handles. We can find the window name using the javascript statement

You can get the value in Chrome by bringing up the Developer window (Right click any where on the page and select Inspect) > Console Tab > Type “window.name” Window Handling Please note that not all windows have the name attribute; though popup windows usually tend of have the attribute. If during switching windows, the window handle is not provided properly, we get errors like NoSuchWindowException, InvalidSwitchToTargetException etc. Also if we switch to the wrong window and try to perform some actions on any element,  we get NoSuchElementException as the page may not have the required element.

SEE ALSO:   How to Handle Alerts and Frames in WebDriver

Wait for new window

Use would sometimes need a method to wait till the new window is opened up. I have given below a method which waits till a new window is opened based on the number of windows. You can modify the method to wait till a window of particular name is opened etc.

 

Switching back to parent window

Once we have completed our activities on the child window, we may have to switch back to the parent window. We may either have to close the child window and switch to parent or just keep the window open.

If we have to close the child window, use driver.close() followed by driver.switchTo().window(“handleOfParentWindow”); 

If we don’t need to close the child window, just use driver.switchTo().window(“handleOfParentWindow”); But keep in mind that the window is still open. So if we open a new window, getWindowHandles() method will return the parent window, the first child window and the newly opened window. So be careful while using a loop to switch to other windows.

SEE ALSO:   What is the difference between POST, PUT and PATCH?

If we properly manage all the open windows, window management is one of the easiest aspects in webdriver.

In the next post, we will look into Alert and Frame Handling.

You may also like...

3 Responses

  1. Ishwarya says:

    Thanks for the very helpful article, which gives us an understanding on switching windows in webdriver.
    Can we do this while on IFrame loading ?
    Thanks in advance.

  1. April 28, 2016

    […] WebDriver Tutorial 4 : Better Window Handling […]

  2. April 28, 2016

    […] Tutorial 4 : Window Handling […]

Leave a Reply

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