所以我想点击这个按钮,如果这是你第一次点击它.将出现一个javascript警告弹出窗口.我一直在使用firebug,但是找不到javascript的位置,我试过了
if EC.alert_is_present:
driver.switch_to_alert().accept()
else:
print("no alert")
Run Code Online (Sandbox Code Playgroud)
如果有一个警告框,上面的代码有效,但如果没有警告框则会抛出错误.即使有其他声明,我甚至尝试过
if EC.alert_is_present:
driver.switch_to_alert().accept()
elif not EC.alert_is_present:
print("no alert")
Run Code Online (Sandbox Code Playgroud)
它抛出了我这个错误
selenium.common.exceptions.NoAlertPresentException: Message: No alert is present
Run Code Online (Sandbox Code Playgroud)
我们如何解决这个问题?
使用try catch,如果Alert不存在,则捕获NoAlertPresentException异常:
from selenium.common.exceptions import NoAlertPresentException
try:
driver.switch_to_alert().accept()
except NoAlertPresentException as e:
print("no alert")
Run Code Online (Sandbox Code Playgroud)