Why switching to alert through selenium is not stable?
For example.
1. Run a code and all good. Everything worked out well.
But if this code is run in a few min
This will click the OK button on the alert:
driver.switch_to.alert.accept()
This will click the CANCEL button on the alert:
driver.switch_to.alert.dismiss()
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()