How would you check if a popup window exists using

2019-07-21 19:23发布

问题:

I am running link tests on an application and one of the links brings up a login popup window. Is there a way to check for that? I tried treating it like an alert but it didn't work.

try
   {
     WebDriverWait wait = new WebDriverWait(driver, 2);
     wait.until(ExpectedConditions.alertIsPresent());
     Alert alert = driver.switchTo().alert();
     alert.accept();
     reportAlertPresent = true;
    }

回答1:

On driver.switchTo().alert(), a org.openqa.selenium.NoAlertPresentException can be thrown, which is an unchecked (i.e. RuntimeException).

You could catch it and possibly do this in a loop.



回答2:

If your popup is inside an iframe then,

 WebElement frameID = driver.findElement(By.(locator));
 driver.switchTo().frame(frameID);

If it is a window, you can use window handles. the below links might help

http://www.thoughtworks.com/products/docs/twist/2.3/help/how_do_i_handle_popup_in_selenium2.html

How to handle Pop-up in Selenium WebDriver using Java