How to handle popup windows in webdriver

15,160 views
Skip to first unread message

Mallikarjun Yalagi

unread,
Jun 21, 2012, 6:32:08 AM6/21/12
to seleniu...@googlegroups.com
Hi,

when i click a popup window will come and need to perform some action on that window.how to handle that popup window using selenium web driver?

Thanks&Regards
Mallikarjun Yalagi

Mike Riley

unread,
Jun 21, 2012, 1:20:30 PM6/21/12
to seleniu...@googlegroups.com
If it is a new window you need to use switchTo().window(handle) to change to it.  The trick is finding the handle.  I use a wrapper routine that does the click and waits for the list of windows to change.  It then uses the new window handle and returns the original window handle to the caller, so the caller knows which handle to switch back to.  The handle is not the title of the window (that would be too easy).

If it is an Alert window you need to use switchTo().alert(), where you can do a getText() from the alert window and do an accept() to dismiss it.

Mike

Mallikarjun Yalagi

unread,
Jun 22, 2012, 1:45:15 AM6/22/12
to seleniu...@googlegroups.com
Hi Mike
  
            Thanks for your reply.It's a new window,when i click a new window will open so need to use switchTo.window(handle) upto here i understood.now my question is what "handle" exactly means??It means window title or id or wt??how to find window title or id ??some says using firebug we can identify window title or id but i am not getting how to identify??I didnt understood the following wt u said  "I use a wrapper routine that does the click and waits for the list of windows to change.  It then uses the new window handle and returns the original window handle to the caller, so the caller knows which handle to switch back to.  The handle is not the title of the window (that would be too easy)." please clarify.

Thanks&Regards
Mallikarjun Yalagi

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/-pF-bAeF0TwJ.
For more options, visit https://groups.google.com/groups/opt_out.

sridhar ..

unread,
Jun 22, 2012, 1:58:50 AM6/22/12
to seleniu...@googlegroups.com
Hi 

we can handle popup windows using swithTo() ---function

driver.swithTo().window(windowId);

we can get windowids using----- driver.getwidnowhandles(); 

Mike Riley

unread,
Jun 22, 2012, 2:08:04 AM6/22/12
to seleniu...@googlegroups.com
I have a method like this (I am doing this from memory, the library with it is at work):
public String clickPopUp(String locator)
{
}

The locator is what I do a click() on to cause the popup window to appear.  Before I do that though I create a List<String> of the current set of window handles and I also save the handle of the current window (very important).  I then do the click() and then loop getting a new list of window handles.  I do a .1 second sleep before getting the new list each time, to give the window time to appear.  As soon as the new list has more handles in it the loop ends.

Now I go through the list of new handles and when I find a handle that was not in the original list I use that to switch to the new window.

Finally I return back to the caller the handle for the window I started in.  When you are done with the popup window and close it (via button click or whatever) you need that old handle to switch back to the original window.

I only have one test where I needed it, but it works great and worked the very first time after I wrote it!

Mike


On Thursday, June 21, 2012 10:45:15 PM UTC-7, Mallikarjun Yalagi wrote:
Hi Mike
  
            Thanks for your reply.It's a new window,when i click a new window will open so need to use switchTo.window(handle) upto here i understood.now my question is what "handle" exactly means??It means window title or id or wt??how to find window title or id ??some says using firebug we can identify window title or id but i am not getting how to identify??I didnt understood the following wt u said  "I use a wrapper routine that does the click and waits for the list of windows to change.  It then uses the new window handle and returns the original window handle to the caller, so the caller knows which handle to switch back to.  The handle is not the title of the window (that would be too easy)." please clarify.

Thanks&Regards
Mallikarjun Yalagi

On Thu, Jun 21, 2012 at 10:50 PM, Mike Riley <lvsk...@cox.net> wrote:
If it is a new window you need to use switchTo().window(handle) to change to it.  The trick is finding the handle.  I use a wrapper routine that does the click and waits for the list of windows to change.  It then uses the new window handle and returns the original window handle to the caller, so the caller knows which handle to switch back to.  The handle is not the title of the window (that would be too easy).

If it is an Alert window you need to use switchTo().alert(), where you can do a getText() from the alert window and do an accept() to dismiss it.

Mike


On Thursday, June 21, 2012 3:32:08 AM UTC-7, Mallikarjun Yalagi wrote:
Hi,

when i click a popup window will come and need to perform some action on that window.how to handle that popup window using selenium web driver?

Thanks&Regards
Mallikarjun Yalagi

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.

Mallikarjun Yalagi

unread,
Jun 22, 2012, 2:10:21 AM6/22/12
to seleniu...@googlegroups.com
Hi siddu,

         Thanks for the reply.you mean we can get windowids using driver.getwindow handles() function and store it in one varible and call that varible in switchTo function right.

Thanks&Regards
Mallikarjun Yalagi

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/DQXCLCo5pvMJ.

Mallikarjun Yalagi

unread,
Jun 22, 2012, 6:19:12 AM6/22/12
to seleniu...@googlegroups.com
mike,

     can you share the code, i tried still not working.

Thanks&Regards
Mallikarjun Yalagi

Mike Riley

unread,
Jun 22, 2012, 1:00:11 PM6/22/12
to seleniu...@googlegroups.com
I have three variations, based on what you are going to click on.  I do use a waitForElementPresent() internal method, which should be obvious what it does based on the name.  Also, I am using WebDriverBackedSelenium, which is why you see a selenium.waitForPageToLoad() in there.  I also have a timer that is available and used to do timeouts to insure we don't end up waiting forever (again, fairly obvious how it works):

Mike

Here is the code:

    /**
     * This is used when clicking on some page element causes a new window to
     * pop up, which can either be a new floating window or a new tab.  It will
     * do the click on the element and then switch to the new window.  It
     * returns the handle for the current window so you can switch back to it
     * once you have finished with the pop up window. It will ignore "Timed out"
     * errors in the belief that they are false<br/>
     * <br/>
     * After we switch to the new window we do a
     * <code>selenium.waitForPageToLoad()</code> before returning to the caller.
     * Be sure to check for the first element you wish to interact with right,
     * or what you think is the last of the dynamic page elements created by
     * JavaScript, after this call returns to be sure it is present using
     * <code>helper.waitForElementPresent()</code>.
     * @param locator The locator string to do the selenium.click() for.
     * @throws Exception
     * @see #waitForElementPresent(java.lang.String)
     * @see #waitForElementPresent(java.lang.String, int)
     * @see #waitForElementPresent(org.openqa.selenium.By)
     * @see #waitForElementPresent(org.openqa.selenium.By, int)
     */
    public String clickAndPopupWindow(String locator) throws Exception
    {
        String newWindow;
        Iterator<String> iterator;
        String oldWindow = driver.getWindowHandle();
        Set<String> oldSet = driver.getWindowHandles();
        Set<String> newSet = oldSet;

        try
        {   // PENDING If this solves the false timeout we need a click() wrapper method
            waitForElementPresent(locator);
            selenium.click(locator);
        }   // try
        catch (Exception ex)
        {   // Check to make sure the exception is valid before throwing it
            if (!ex.getMessage().startsWith(timedOutPrefix))
                throw ex;
        }   // catch

        timer.setInitialDelay(timeout);
        timer.start();
        while (newSet.size() == oldSet.size() || timerExpired)
        {   // Wait for the new window to be displayed
            Thread.sleep(100);  // Wait .1 seconds
            newSet = driver.getWindowHandles(); // See if the new window is up
        }   // while

        timer.stop();
        if (timerExpired)
            throw new Exception("No new window was created after clicking: " +
                                locator);

        iterator = newSet.iterator();
        do
        {   // Find which one is the new window
            newWindow = iterator.next();
        } while (oldSet.contains(newWindow) && iterator.hasNext());

        driver.switchTo().window(newWindow);
        selenium.waitForPageToLoad(timeoutString);
        return oldWindow;
    }   // clickAndPopupWindow(String locator)

    /**
     * This is used when clicking on some page element causes a new window to
     * pop up, which can either be a new floating window or a new tab.  It will
     * do the click on the element and then switch to the new window.  It
     * returns the handle for the current window so you can switch back to it
     * once you have finished with the pop up window. It will ignore "Timed out"
     * errors in the belief that they are false<br/>
     * <br/>
     * After we switch to the new window we do a
     * <code>selenium.waitForPageToLoad()</code> before returning to the caller.
     * Be sure to check for the first element you wish to interact with right,
     * or what you think is the last of the dynamic page elements created by
     * JavaScript, after this call returns to be sure it is present using
     * <code>helper.waitForElementPresent()</code>.
     * @param locator The By class locator to do the
     *        <code>driver.findElement().click()</code> for.
     * @throws Exception
     * @see #waitForElementPresent(java.lang.String)
     * @see #waitForElementPresent(java.lang.String, int)
     * @see #waitForElementPresent(org.openqa.selenium.By)
     * @see #waitForElementPresent(org.openqa.selenium.By, int)
     */
    public String clickAndPopupWindow(By locator) throws Exception
    {
        String newWindow;
        Iterator<String> iterator;
        String oldWindow = driver.getWindowHandle();
        Set<String> oldSet = driver.getWindowHandles();
        Set<String> newSet = oldSet;

        try
        {   // PENDING If this solves the false timeout we need a click() wrapper method
            waitForElementPresent(locator);
            driver.findElement(locator).click();
        }   // try
        catch (Exception ex)
        {   // Check to make sure the exception is valid before throwing it
            if (!ex.getMessage().startsWith(timedOutPrefix))
                throw ex;
        }   // catch

        timer.setInitialDelay(timeout);
        timer.start();
        while (newSet.size() == oldSet.size() || timerExpired)
        {   // Wait for the new window to be displayed
            Thread.sleep(100);  // Wait .1 seconds
            newSet = driver.getWindowHandles(); // See if the new window is up
        }   // while

        timer.stop();
        if (timerExpired)
            throw new Exception("No new window was created after clicking: " +
                                locator.toString());

        iterator = newSet.iterator();
        do
        {   // Find which one is the new window
            newWindow = iterator.next();
        } while (oldSet.contains(newWindow) && iterator.hasNext());

        driver.switchTo().window(newWindow);
        selenium.waitForPageToLoad(timeoutString);
        return oldWindow;
    }   // clickAndPopupWindow(By locator)

    /**
     * This is used when clicking on some page element causes a new window to
     * pop up, which can either be a new floating window or a new tab.  It will
     * do the click on the element and then switch to the new window.  It
     * returns the handle for the current window so you can switch back to it
     * once you have finished with the pop up window. It will ignore "Timed out"
     * errors in the belief that they are false<br/>
     * <br/>
     * After we switch to the new window we do a
     * <code>selenium.waitForPageToLoad()</code> before returning to the caller.
     * Be sure to check for the first element you wish to interact with right,
     * or what you think is the last of the dynamic page elements created by
     * JavaScript, after this call returns to be sure it is present using
     * <code>helper.waitForElementPresent()</code>.
     * @param element The <code>WebElement</code> to do the <code>.click()</code> for.
     * @throws Exception
     * @see #waitForElementPresent(java.lang.String)
     * @see #waitForElementPresent(java.lang.String, int)
     * @see #waitForElementPresent(org.openqa.selenium.By)
     * @see #waitForElementPresent(org.openqa.selenium.By, int)
     */
    public String clickAndPopupWindow(WebElement element) throws Exception
    {
        String newWindow;
        Iterator<String> iterator;
        String oldWindow = driver.getWindowHandle();
        Set<String> oldSet = driver.getWindowHandles();
        Set<String> newSet = oldSet;

        try
        {   // PENDING If this solves the false timeout we need a click() wrapper method
            element.click();
        }   // try
        catch (Exception ex)
        {   // Check to make sure the exception is valid before throwing it
            if (!ex.getMessage().startsWith(timedOutPrefix))
                throw ex;
        }   // catch

        timer.setInitialDelay(timeout);
        timer.start();
        while (newSet.size() == oldSet.size() || timerExpired)
        {   // Wait for the new window to be displayed
            Thread.sleep(100);  // Wait .1 seconds
            newSet = driver.getWindowHandles(); // See if the new window is up
        }   // while

        timer.stop();
        if (timerExpired)
            throw new Exception("No new window was created after clicking: " +
                                element.toString());

        iterator = newSet.iterator();
        do
        {   // Find which one is the new window
            newWindow = iterator.next();
        } while (oldSet.contains(newWindow) && iterator.hasNext());

        driver.switchTo().window(newWindow);
        selenium.waitForPageToLoad(timeoutString);
        return oldWindow;
    }   // clickAndPopupWindow(WebElement element)



On Friday, June 22, 2012 3:19:12 AM UTC-7, Mallikarjun Yalagi wrote:
mike,

     can you share the code, i tried still not working.

Thanks&Regards
Mallikarjun Yalagi

On Fri, Jun 22, 2012 at 11:40 AM, Mallikarjun Yalagi <yalagi...@gmail.com> wrote:
Hi siddu,

         Thanks for the reply.you mean we can get windowids using driver.getwindow handles() function and store it in one varible and call that varible in switchTo function right.

Thanks&Regards
Mallikarjun Yalagi
On Fri, Jun 22, 2012 at 11:28 AM, sridhar .. <sidd...@gmail.com> wrote:
Hi 

we can handle popup windows using swithTo() ---function

driver.swithTo().window(windowId);

we can get windowids using----- driver.getwidnowhandles(); 


On Thursday, June 21, 2012 4:02:08 PM UTC+5:30, Mallikarjun Yalagi wrote:
Hi,

when i click a popup window will come and need to perform some action on that window.how to handle that popup window using selenium web driver?

Thanks&Regards
Mallikarjun Yalagi

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.

Mike Riley

unread,
Jun 25, 2012, 2:50:59 PM6/25/12
to seleniu...@googlegroups.com
I believe an empty string will also switch back to the main window.  If that doesn't work you should be able to do a list of the window handles.  Assuming there is only one you would switch to it.  If you can have more than one I would do a variation of what I did to find the popup window.  Keep your original list, prior to the popup window being displayed, and compare it with your current list.  Whatever window in the current list that is not in the old list is likely the window you need to switch to.

Mike

On Sunday, June 24, 2012 8:48:16 PM UTC-7, simply_tester wrote:
Hi Mike,

Your explanation is very clear. Thank you. However, I'm having a slightly different issue. In my case, after switching to a pop-up and performing some actions there (sign-in to a page), pop-up automatically closes, and the main window refreshes (user is signed in and gets redirected to a different page). So, when I try to getWindowHandle on the refreshed page - I get a "driver not found... The browser is probably closed" error. If I try to switchTo previously stored main window handle, I get a sign-in page loaded (expected) when user is actually signed-in. My question is, what is the proper way to switch to main window (once again, main window is a sign-in window that refreshes to a different page after user sign-in through a pop-up) after the sign-in pop-up closes?

Thanks in advance,

simply_tester

unread,
Jun 25, 2012, 4:51:03 PM6/25/12
to seleniu...@googlegroups.com
Thanks for replying. I've tried that (getting a list of all handles) and since there is only one, use getWindowHandle, assign in to a string and then, use switchTo().window(string) after the pop-up closes, however, this is where my test fails with the error: has no driver.. The browser window may have been closed. Any ideas?

Thanks,

Mallikarjun Yalagi

unread,
Jun 26, 2012, 6:28:53 AM6/26/12
to seleniu...@googlegroups.com
mike,

       Really your explanation and your code helped me to solve the popup's.Thanks a lot for your help.

Thanks&Regards
Mallikarjun Yalagi

To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/bCKTw5Z6XH4J.

Mike Riley

unread,
Jun 26, 2012, 1:06:45 PM6/26/12
to seleniu...@googlegroups.com
Please post the full stack trace and your selenium code where it happens so we can all see it.  Hopefully that will give someone an idea as to what is going on.

Mike

simply_tester

unread,
Jun 26, 2012, 6:42:25 PM6/26/12
to seleniu...@googlegroups.com
Problem solved. I was extending another class that was pointing to the wrong URL. Thanks for the help.

Mallikarjun Yalagi

unread,
Jul 6, 2012, 2:06:21 AM7/6/12
to seleniu...@googlegroups.com
mike,

      if i run the same code to handle popup's in internet explorer  it's not working,is there any change need to be done in code or wt??

Thanks&Regards
Mallikarjun Yalagi

To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/agM_2DdLsyQJ.

Mike Riley

unread,
Jul 6, 2012, 12:04:15 PM7/6/12
to seleniu...@googlegroups.com
My code was tested using IE.  I actually just this past week finally ran it on Firefox and Chrome and it worked there, too.

A possibility is that on IE your developers have implemented the popup in a different way for IE.  I would suggest getting a look at how the code looks in IE (using the Developer tool via <F12>) to see if it looks different that in the other browsers.

In my case the popup I am dealing with is created using JQuery (I believe, or some other JavaScript package).

If you do find something different about it, try showing it to us here so we can help you figure it out.

Mike

Mallikarjun Yalagi

unread,
Jul 9, 2012, 6:13:50 AM7/9/12
to seleniu...@googlegroups.com
mike,

    what was the version of IE ??i am using 8 and i will check whether the developer's implemented the popup in a different way for IE.

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/AKGwXa3eRUgJ.

To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en-US.

Mike Riley

unread,
Jul 9, 2012, 12:11:38 PM7/9/12
to seleniu...@googlegroups.com
I ran it on IE 7, 8, and 9.

You developer may be using a canned set of JavaScript routines, and so may not know how the popup was implemented, although they should be able to look at it and find out.  That is why I suggested looking at it in IE when you do it manually using the <F12> key to bring up the developer view in IE.  You can then compare that with how Firebug shows the same view in Firefox, or use the Chrome developer tool if you are using Chrome.

Mike

Mallikarjun Yalagi

unread,
Jul 10, 2012, 10:29:37 AM7/10/12
to seleniu...@googlegroups.com
mike,

       now it's able to recognize the popup but not able to recognize the element present in the popup window in IE so how to identify the identifier i.e xpath or css or id in IE. is it possible to identify with developer tool?

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/hm2WHZOFPjgJ.

To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.

Mike Riley

unread,
Jul 11, 2012, 12:34:42 PM7/11/12
to seleniu...@googlegroups.com
That sounds like you might have frames in the popup, but hard to say without seeing the HTML.  If it is in a frame you will need to switch to that frame first.  There are lots of posts here that explain how to do that.

Mike

Mallikarjun Yalagi

unread,
Jul 12, 2012, 9:21:02 AM7/12/12
to seleniu...@googlegroups.com
mike,

    i didnt find any post related to frames,if you know please explain me or give me the link.

Your help will be appreciated.

Thanks&Regards
Mallikarjun Yalagi


To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/D2naCq1m1-0J.

To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.

Peter Gale

unread,
Jul 12, 2012, 9:23:51 AM7/12/12
to Selenium Users
What have you done to search for related posts?


Date: Thu, 12 Jul 2012 18:51:02 +0530
Subject: Re: [selenium-users] Re: How to handle popup windows in webdriver
From: yalagi...@gmail.com
To: seleniu...@googlegroups.com

Mallikarjun Yalagi

unread,
Jul 12, 2012, 9:26:22 AM7/12/12
to seleniu...@googlegroups.com
what you will do to search the posts??

Thanks&Regards
Mallikarjun Yalagi

Peter Gale

unread,
Jul 12, 2012, 9:55:06 AM7/12/12
to Selenium Users
You mean to say that you haven't really done a search on the forum at all and you're asking me how to tell you to you to do a search?


Date: Thu, 12 Jul 2012 18:56:22 +0530

Mike Riley

unread,
Jul 12, 2012, 12:26:31 PM7/12/12
to seleniu...@googlegroups.com
Since a lot of posts in this forum are for the IDE or Selenium 1.0 (RC) issues, you would do better to use the webdriver forum so you only see WebDriver solutions.

Simply put frame or frames in as the word to search for in the "Search for topics" box and you will get a lot of hits.

Mike


On Thursday, July 12, 2012 6:26:22 AM UTC-7, Mallikarjun Yalagi wrote:
what you will do to search the posts??

Thanks&Regards
Mallikarjun Yalagi

On Thu, Jul 12, 2012 at 6:53 PM, Peter Gale <peterjef...@hotmail.co.uk> wrote:
What have you done to search for related posts?


Date: Thu, 12 Jul 2012 18:51:02 +0530
Subject: Re: [selenium-users] Re: How to handle popup windows in webdriver
From: yalagi...@gmail.com

Mallikarjun Yalagi

unread,
Jul 13, 2012, 1:51:26 AM7/13/12
to seleniu...@googlegroups.com
peter,

 Just checked your mentality,if ppl dont have work to do ,then such things they will do.

Thanks&Regards
Mallikarjun Yalagi

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/-kMHhEy-vfgJ.

To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.

Peter Gale

unread,
Jul 13, 2012, 3:24:35 AM7/13/12
to Selenium Users
> peter,
> Just checked your mentality,if ppl dont have work to do ,then such things they will do.

I don't understand what you're trying to say there, Mallikarjun. Perhaps you could explain better.


Date: Fri, 13 Jul 2012 11:21:26 +0530

Subject: Re: [selenium-users] Re: How to handle popup windows in webdriver
From: yalagi...@gmail.com

Krishnan Mahadevan

unread,
Jul 13, 2012, 3:33:26 AM7/13/12
to seleniu...@googlegroups.com
Guys,
Lets not get personal.
Lets call it peace :) Its Friday... :) Lets not divert the issue that is being discussed in the thread.

A humble request !

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

Rajendra Prasad

unread,
Nov 6, 2012, 6:56:20 AM11/6/12
to seleniu...@googlegroups.com
can you please share the code for handling pop ups


On Thursday, June 21, 2012 10:50:30 PM UTC+5:30, Mike Riley wrote:
If it is a new window you need to use switchTo().window(handle) to change to it.  The trick is finding the handle.  I use a wrapper routine that does the click and waits for the list of windows to change.  It then uses the new window handle and returns the original window handle to the caller, so the caller knows which handle to switch back to.  The handle is not the title of the window (that would be too easy).

If it is an Alert window you need to use switchTo().alert(), where you can do a getText() from the alert window and do an accept() to dismiss it.

Mike

Reply all
Reply to author
Forward
0 new messages