How to check if an alert exists using WebDriver?

后端 未结 9 1742
名媛妹妹
名媛妹妹 2020-12-01 09:09

I need to check the existence of Alert in WebDriver.

Sometimes it pops up an alert but sometimes it will not pop up. I need to check if the alert exists first, then

9条回答
  •  一生所求
    2020-12-01 09:33

    I found catching exception of driver.switchTo().alert(); is so slow in Firefox (FF V20 & selenium-java-2.32.0).`

    So I choose another way:

        private static boolean isDialogPresent(WebDriver driver) {
            try {
                driver.getTitle();
                return false;
            } catch (UnhandledAlertException e) {
                // Modal dialog showed
                return true;
            }
        }
    

    And it's a better way when most of your test cases is NO dialog present (throwing exception is expensive).

提交回复
热议问题