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

Viewed 18735

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

2 Answers

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
Related