Search code examples
selenium-webdriverautomated-testsbdd

Workaround for alert fail to disconnect Webdriver


I am automating an website using JBehave and Selenium Webdriver, one of the functionality is to logout the system. It works for most times, but eventually it doesn't, because the browser shows an alert saying: "Fail to disconnect" and it throws the following exception:

org.openqa.selenium.UnhandledAlertException: unexpected alert open

Is there any workaround for this?


Solution

  • You need to switch to the Alert first and then accept or dismiss it.

    Try this-

            IAlert alert = driver.SwitchTo().Alert();
    
            //Accept Alert 
            alert.Accept();
    
            //Or
    
            //Dismiss Alert 
            alert.Dismiss();