java - 为什么我无法关闭警报弹出窗口?

标签 java selenium selenium-webdriver webdriver

<强>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);
    
  • 有什么想法可以解决这个问题吗? enter image description here 谢谢你的帮助

    最佳答案

    由于您只接受弹出窗口。要关闭弹出窗口,我们必须使用警报的关闭方法 详情请关注以下代码

    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/

    相关文章:

    javascript - Selenium webdriver driver.findElement() 方法返回对象 - 为什么?我做错了什么?

    java - AspectJ - 为什么是 "advice defined in XYZ has not been applied"?

    python - 使用 Selenium webdriver python 绑定(bind)从 Firefox 捕获控制台日志输出

    java - 将一个表中的数百万行与另一个表中的数百万行进行比较的最快方法

    python - Selenium:在谷歌浏览器中上传文件

    linux - 无法在没有 GUI 的 Linux 上使用 htmlSuite 运行 Selenium

    java - Selenium 中的 Apache POI 文本格式不佳

    c# - 如何在 C# 中使用相对路径实例化新的 ChromeDriver?

    Java继承类

    java - NullPointerException 而没有什么是 null