Why switching to alert through selenium is not stable?

As per your code block there are a couple of issues which you need to address as follows :

  • Switching to an Alert : The method switch_to_alert() is Deprecated and you should be using switch_to.alert instead. The API Docs clearly mentions the following :

     def switch_to_alert(self):
         """ Deprecated use driver.switch_to.alert
         """
         warnings.warn("use driver.switch_to.alert instead", DeprecationWarning)
         return self._switch_to.alert
    
  • Wait for the Alert to be present : You should always induce WebDriverWait for the Alert to be present before invoking accept() or dismiss() as follows :

    WebDriverWait(driver, 5).until(EC.alert_is_present).dismiss()
    

Leave a Comment