Selenium UnexpectedAlertPresentException: 处理网页弹出的意外提示
The error message suggests that an unexpected alert was encountered while trying to find elements on the webpage. The alert text is empty, indicating that no specific message is displayed in the alert. The error message also mentions '是什么原因' which means 'What is the reason' in Chinese.
To resolve this issue, you can handle the unexpected alert by using the Alert class in Selenium. Here's an example of how you can handle the alert:
from selenium.webdriver.common.alert import Alert
# Find elements
elements = driver.find_elements(By.XPATH, "//button[text()='确认']")
# Handle unexpected alert
try:
for element in elements:
element.click()
except UnexpectedAlertPresentException:
alert = Alert(driver) # Switch to the alert
alert.dismiss() # Close the alert without accepting it
By using the try-except block, you can catch the UnexpectedAlertPresentException and handle the alert accordingly. In the example above, the alert is dismissed using the dismiss() method of the Alert class. You can modify the handling logic based on your specific requirements.
原文地址: https://www.cveoy.top/t/topic/plcV 著作权归作者所有。请勿转载和采集!