Why switching to alert through selenium is not stable?

前端 未结 2 1719
不思量自难忘°
不思量自难忘° 2020-12-04 01:28

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

相关标签:
2条回答
  • 2020-12-04 01:49

    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()     
    
    0 讨论(0)
  • 2020-12-04 02:03

    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()
      
    0 讨论(0)
提交回复
热议问题