Java 您将如何使用 selenium webdriver 检查弹出窗口是否存在?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24911681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:33:55  来源:igfitidea点击:

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

javaselenium-webdriverpopupwindow

提问by John

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;
    }

回答by Dormouse

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

On driver.switchTo().alert()org.openqa.selenium.NoAlertPresentException可以抛出 a ,这是一个未经检查的(即RuntimeException)。

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

您可以捕获它并可能在循环中执行此操作。

回答by Vignesh Paramasivam

If your popup is inside an iframe then,

如果您的弹出窗口在 iframe 内,则

 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

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

如何使用 Java 处理 Selenium WebDriver 中的弹出窗口