为什么通过selenium切换到alert不稳定?
例如。
1. 运行代码,一切顺利。一切都很顺利。
但如果这段代码在几分钟内运行,那么可能会出现错误。
例如,没有可以单击的元素。等等。
2. 在一个站点上有一个警报窗口。
alert = driver.switch_to_alert()
alert.dismiss()
所以我关闭它。但他通过时间来工作。一切都很好,然后出现错误。
for al in range(3):
try:
alert = driver.switch_to_alert()
alert.dismiss()
time.sleep(randint(1, 3))
except:
pass
我写了,一切都按预期进行。
但我认为这并不美丽。
为什么一切都这么不稳定?
非常感谢。
根据您的代码块,您需要解决以下几个问题:
-
切换到警报 https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.alert.html#module-selenium.webdriver.common.alert: 方法switch_to_alert()
is 已弃用你应该使用switch_to.alert
反而。 API 文档明确提到了以下内容:
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
-
等待警报出现 https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html#selenium.webdriver.support.expected_conditions.alert_is_present: 你应该总是诱导WebDriver等待为了Alert to be present在调用之前accept()
or dismiss()
如下 :
WebDriverWait(driver, 5).until(EC.alert_is_present).dismiss()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)