在处理UnexpectedAlertPresentException期间,python中会发生NoAlertPresentException

2024-04-23 21:20:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理经常发生的意外AlertPresentException,所以我使用了try-except代码来处理它。我正要返回意外警报的内容,所以我的代码是

wait = WebDriverWait(driver2, 10)
try:
    element = wait.until(EC.element_to_be_clickable((By.NAME, 'wlike_limit')))
except UnexpectedAlertPresentException:
    print("UnexpectedAlertPresentException, ",driver2.switch_to.alert.text)

但在打印意外警报的文本时,它也会给我“NoAlertPresentException”

UnexpectedAlertPresentException           Traceback (most recent call last)
<ipython-input-52-543c0fff5a64> in <module>
     16 try:
---> 17    element = wait.until(EC.element_to_be_clickable((By.NAME, 'wlike_limit')))
     18 except UnexpectedAlertPresentException:

UnexpectedAlertPresentException: Alert Text: 
Message: unexpected alert open: {Alert text: }
  (Session info: chrome=84.0.4147.125)


During handling of the above exception, another exception occurred:

NoAlertPresentException                   Traceback (most recent call last)
<ipython-input-52-543c0fff5a64> in <module>
     17    element = wait.until(EC.element_to_be_clickable((By.NAME, 'wlike_limit')))
     18 except UnexpectedAlertPresentException:
---> 19    print("UnexpectedAlertPresentException, ",driver2.switch_to.alert.text)
     20 

有人能帮我处理这个问题吗


Tags: tonamebyelementbeuntillimitwait
1条回答
网友
1楼 · 发布于 2024-04-23 21:20:18

此错误消息

NoAlertPresentException                   Traceback (most recent call last)

…意味着虽然您试图处理Alert,但没有这样的alert


分析

这行代码:

except UnexpectedAlertPresentException:
        print("UnexpectedAlertPresentException, ",driver2.switch_to.alert.text)
        

当您的代码块面对UnexpectedAlertPresentException时,将切换到警报并检索文本。然而,目前似乎没有警报

现在在try{}块中:

element = wait.until(EC.element_to_be_clickable((By.NAME, 'wlike_limit')))

此行不会引发任何警报,因为您没有与元素交互。因此,捕捉UnexpectedAlertPresentException对我来说似乎没有什么用处


结论

处理UnexpectedAlertPresentExceptiontry-catch{}块对我来说似乎没有用处。也许您以前的代码块可以提供一些提示,说明您希望在哪些情况下处理UnexpectedAlertPresentException


参考资料

您可以在以下内容中找到一些相关讨论:

相关问题 更多 >