3

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. :)

Community
  • 1
  • 1
Manuli
  • 1,203
  • 4
  • 20
  • 43

2 Answers2

2

If it's a native, browser alert (= a popup) you can do the following:

try{
    driver.switchTo().alert();
    // If it reaches here, it found a popup
} catch(NoALertPresentException e){}

What it looks like you're actually dealing with is an iframe which you can do the following after getting the attribute value of the "iframe" attribute:

driver.switchTo.frame("ValueOfIframe");
// Treat as normal webpage. Now in iframe scope
driver.switchTo.defaultContent(); // To return back to normal page scope
code_disciple1
  • 121
  • 1
  • 11
1
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);
    }
}
Manuli
  • 1,203
  • 4
  • 20
  • 43