Search code examples
javaselenium

How to check if a popup window is open or not using Selenium WebDriver in Java


I'm trying to check whether the popup window I want to open is opened or not.

I have checked some question answers like

How would you check if a popup window exists using selenium webdriver?

But, nothings helped to solve the problem.

Here, first I open the login window by clicking the login button.

driver.findElement(By.xpath("//a[@id='login_btn']")).click(); // Click Login Button

login

I even tried getPageSource() but, it seems not working.

Any kind of help would be appreciated.

Thanks in advance. :)


Solution

  • String mwh=driver.getWindowHandle();
    

    Now try to open the popup window by performing some action:

    driver.findElement(By.xpath("")).click();
    
    Set s=driver.getWindowHandles(); //this method will gives you the handles of all opened windows
    
    Iterator ite=s.iterator();
    
    while(ite.hasNext())
    {
        String popupHandle=ite.next().toString();
        if(!popupHandle.contains(mwh))
        {
            driver.switchTo().window(popupHandle);
            /**/here you can perform operation in pop-up window**
            //After finished your operation in pop-up just select the main window again
            driver.switchTo().window(mwh);
        }
    }