在处理UnexpectedAlertPresentException的过程中,NoAlertPresentException发生在python selenium
During handling UnexpectedAlertPresentException, NoAlertPresentException occurs in python selenium
我正在处理经常发生的 UnexpectedAlertPresentException,所以我编写了 try-except 代码来处理它。我正要 return Unexpected Alert 的内容,所以我的代码是
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
谁能帮我解决这个问题?
这个错误信息...
NoAlertPresentException Traceback (most recent call last)
...意味着尽管您试图处理 but there was no such .
分析
这行代码:
except UnexpectedAlertPresentException:
print("UnexpectedAlertPresentException, ",driver2.switch_to.alert.text)
当您的代码块遇到 UnexpectedAlertPresentException 时, 将切换到警报并检索文本。但是似乎没有警报。
现在在 try{}
块中:
element = wait.until(EC.element_to_be_clickable((By.NAME, 'wlike_limit')))
此行不会引发任何警报,因为您没有与该元素进行交互。因此,捕捉 UnexpectedAlertPresentException
对我来说似乎没有用。
结论
用于处理 UnexpectedAlertPresentException
的 try-catch{}
块对我来说似乎没有用。也许您之前的代码块可以提供一些提示,您希望在哪些情况下处理 UnexpectedAlertPresentException.
参考资料
您可以在以下位置找到一些相关讨论:
- Python click button on alert
在我身边,当我试图处理 UnexpectedAlertPresentException
时。然后我注意到 Selenium 自动关闭警报 pop-up。因此在这种情况下,我只是重试我的脚本并且它有效!
def some_function:
try:
return driver.find_element_by_css_selector(
"#elementID"
).text
except UnexpectedAlertPresentException:
# In this case, no need to call driver.switch_to.alert.accept()
# or driver.switch_to.alert.dismiss()
time.sleep(5) # very important to wait for selenium to finish the action
driver.find_element_by_css_selector(
"#elementID"
).text
我正在处理经常发生的 UnexpectedAlertPresentException,所以我编写了 try-except 代码来处理它。我正要 return Unexpected Alert 的内容,所以我的代码是
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
谁能帮我解决这个问题?
这个错误信息...
NoAlertPresentException Traceback (most recent call last)
...意味着尽管您试图处理
分析
这行代码:
except UnexpectedAlertPresentException:
print("UnexpectedAlertPresentException, ",driver2.switch_to.alert.text)
当您的代码块遇到 UnexpectedAlertPresentException 时,将切换到警报并检索文本。但是似乎没有警报。
现在在 try{}
块中:
element = wait.until(EC.element_to_be_clickable((By.NAME, 'wlike_limit')))
此行不会引发任何警报,因为您没有与该元素进行交互。因此,捕捉 UnexpectedAlertPresentException
对我来说似乎没有用。
结论
用于处理 UnexpectedAlertPresentException
的 try-catch{}
块对我来说似乎没有用。也许您之前的代码块可以提供一些提示,您希望在哪些情况下处理 UnexpectedAlertPresentException.
参考资料
您可以在以下位置找到一些相关讨论:
- Python click button on alert
在我身边,当我试图处理 UnexpectedAlertPresentException
时。然后我注意到 Selenium 自动关闭警报 pop-up。因此在这种情况下,我只是重试我的脚本并且它有效!
def some_function:
try:
return driver.find_element_by_css_selector(
"#elementID"
).text
except UnexpectedAlertPresentException:
# In this case, no need to call driver.switch_to.alert.accept()
# or driver.switch_to.alert.dismiss()
time.sleep(5) # very important to wait for selenium to finish the action
driver.find_element_by_css_selector(
"#elementID"
).text