Friday, April 20, 2012

How to find a popup window if it does not have a name


When using Selenium 2.0 (WebDriver) you will sometimes find clicking a link opens a popup window. Before you can interact with the elements on the new window you need to switch to the new window. If the new window has a name you can use the name to switch to the popup window:

    driver.switchTo().window(name);

However, if the window does not have a name you need to use the window handle. The problem is that every time you run the code the window handle will change. So you need to find the window handle for the popup window. To do this I use getWindowHandles() to find all the open windows, I click the link to open the new window. Next I use getWindowHandles() to get a second set of window handles. If I remove all the window handles of the first set from the second set I should end up with a set with only one element. That element will be the handle of the popup window.

Here is the code:
String getPopupWindowHandle(WebDriver driver, WebElement link) {

    // get all the window handles before the popup window appears
    Set<String> beforePopup = driver.getWindowHandles();

    // click the link which creates the popup window
    link.click();

    // get all the window handles after the popup window appears
    Set<String> afterPopup = driver.getWindowHandles();

    // remove all the handles from before the popup window appears
    afterPopup.removeAll(beforePopup);

    // there should be only one window handle left
    if(afterPopup.size() == 1) {
        return (String)afterPopup.toArray()[0];
    }
    return null;
}
To use this I simply call it with the WebElement which clicking opens the new window. You want to add some error handling to this but the basic idea of finding the popup window is here. To use the method I would use:
String currentWindowHandle = driver.getWindowHandle();
String popupWindowHandle = getPopupWindowHandle(driver, link);
driver.switchTo().window(popupWindowHandle);
// do stuff on the pop window
// close the popup window
driver.switchTo().window(currentWindowHandle)

9 comments:

  1. Hi,
    Your Approach for handling Pop and Windows is very much help full for me.

    Thanks

    Happy Testing

    ReplyDelete
  2. Thanks for the input. It helped a lot in shifting to new popup window which has no name.

    ReplyDelete
  3. Hi Darrell,
    Your code works perfect with single popup window but could not able to use for multiple popup since the usage of removeAll();
    Can you suggest a solution for multiple popup without using iterator.next(); since it is not consistent on InternetExplorerDriver();

    Flow in navigation order:
    1.Main Window
    2.First Popup Window
    3.Second Popup Window
    4.First Popup Window
    5.Main Window

    ReplyDelete
  4. Shahul,

    The code helps you to find a new popup window. It is not meant to help you navigate a sequence of popups. Your example is missing some details. If you actual mean:

    1. go to main window
    2. click something which opens first popup window
    3. go to first popup window
    4. click something which opens second popup window
    5. interact with second popup window
    6. close second popup window
    7. go to first popup window
    8. close first popup window
    9. go to main window

    My code would be used as follows:

    String mainHandle = driver.getWindowHandle();
    WebElement firstPopup = driver.findElement(...);
    String firstPopupHandle = getPopupWindowHandle(driver, firstPopup); // does click
    driver.switchTo().window(firstPopupHandle);
    WebElement secondPopup = driver.findElement(...);
    String secondPopupHandle = getPopupWindowHandle(driver, secondPopup); // does click
    driver.switchTo().window(secondPopupHandle);
    // interact with second popup window
    // do something to close second window
    driver.switchTo().window(firstPopupHandle);
    // do something to close first window
    driver.switchTo().window(mainHandle);

    This assumes that the link/button to create the second popup window is on the first popup window. If you have any more questions, please post them to the WebDriver Google Group. It is easier to have an interactive conversation there.

    ReplyDelete
  5. Thanks a ton. It perfectly worked.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Thanks Darrel for the post,

    I am having problem in handling popups in my application,
    I have put SOP(driver.getWindowHandles.size())
    before and after popup is opened ,
    the value remains 1

    So driver is not getting any popup to switch.
    Please help me in fixing this issue, as it becomes bottleneck for my testing

    ReplyDelete
  8. where is getPopupWindowHandle defined?

    ReplyDelete
  9. That's Good Approach to handle Pop-up.

    I'm currently facing problem with the flash player settings .

    Could you please let me know, how to handle for those kind of pop-up.

    Because selenium IDE is not recognizing the actions ,which i'm doing on that window.

    ReplyDelete

Note: Only a member of this blog may post a comment.