<强>1。为什么我无法关闭警报弹出窗口?
- 我尝试过警报等,它会关闭弹出窗口,但后来我得到了弹出窗口不关闭的奇怪测试/
<强>3。我的代码:
public void closeAlertPopupBox() throws AWTException, InterruptedException {
try {
Alert alert = this.wait.until(ExpectedConditions.alertIsPresent());
alert.accept();
System.out.println("The popup has been successfully closed");
} catch (UnhandledAlertException f) {
Alert alert = this.driver.switchTo().alert();
alert.accept();
} catch (Exception e) {
System.out.println("Unable to close the popup");
Assert.assertFalse(true, "Unable to close the popup, Exception: " + e.getMessage());
}
}
上面的代码可以工作,但我得到了一个奇怪的弹出窗口,它不会关闭,并且测试会失败。
使用以下所需功能可以正常工作,但随后我在 TestNG 上收到错误,提示存在“意外弹出窗口”,但代码仍然接受并关闭弹出窗口。
我的代码:
System.setProperty("webdriver.chrome.driver", Constant.CHROME_DRIVER_DIRECTORY); DesiredCapabilities caps = DesiredCapabilities.chrome(); LoggingPreferences logPrefs = new LoggingPreferences(); logPrefs.enable(LogType.BROWSER, Level.ALL); UnexpectedAlertBehaviour.ACCEPT); caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs); webdriver = new ChromeDriver(caps);
最佳答案
由于您只接受弹出窗口。要关闭弹出窗口,我们必须使用警报的关闭方法 详情请关注以下代码
Alert alert= driver.switchTo().alert();
alert.dismiss();
如果问题仍然存在,那么您可以尝试处理相同的问题
Alert alert= driver.switchTo().alert();
Thread.sleep(500)
alert.dismiss();
关于java - 为什么我无法关闭警报弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42203195/