How to wait for a popup window to appear?

70 views
Skip to first unread message

Nitin Goyal

unread,
May 19, 2015, 11:53:16 AM5/19/15
to seleniu...@googlegroups.com
How to wait for a popup window to appear?
STR:
I am clicking on a button(which is already on a popup window) which opens a new popup window. I want to switch to newly opened window.
But the issue is the popup window takes few seconds to load. I can't switch to that window until or unless it is displayed.
I am trying to use explicit wait but the issue with explicit wait is that it waits for webelement or some other stuff not for a string. 

How can i wait for a popup window. Please suggest if anyone knows.

Ranjit Kundu

unread,
May 20, 2015, 9:47:17 AM5/20/15
to seleniu...@googlegroups.com
 String mainwindow=driver.getWindowHandle();
 driver.findElement(By.linkText("Button1")).click(); // to open the first window 

       for(String winHandle :driver.getWindowHandles())
           {
             driver.switchTo().window(winHandle);
                if(driver.getTitle().equals("The sub window title you want to work on"))
                    {
                       System.out.println("You are in required window");
                       driver.findElement(By.linkText("Button2")).click();
                       break;
                    }
                       else
                       {
                       System.out.println("Title of the page after - switchingTo: " + driver.getTitle());
                }
            }
            driver.switchTo().window(mainwindow);
            driver.quit();


Let me know if it works for you.

Brian Smith

unread,
May 20, 2015, 9:07:59 PM5/20/15
to seleniu...@googlegroups.com
Are you referring to an element in the DOM dynamically displayed due to a button trigger or method, or an actual window displayed by the browser?

R. Sowmya

unread,
May 22, 2015, 1:16:43 AM5/22/15
to seleniu...@googlegroups.com
Below mentioned code will wait before switching to new window.before clicking the button just include 

    oldwindows = driver.getWindowHandles().size(); and pass driver and this as parameter to below code


public void switchToNewWindow(WebDriver driver, int oldwindows)
{
int count=0;
while(oldwindows==driver.getWindowHandles().size())
{
sleep(2000);
count++;
if(count==150)
{ Assert.fail("New window is not opened until 5 minutes");
break;
}
}
for(String winHandle : driver.getWindowHandles())
{
driver.switchTo().window(winHandle);
        }
}

On Tuesday, May 19, 2015 at 9:23:16 PM UTC+5:30, Nitin Goyal wrote:
Reply all
Reply to author
Forward
0 new messages