NoAlertPresentException Traceback most recent call lastInput In 20 in cell line 6 4 driver=webdriverChrome 5 drivergethttpbjbendibaocomnewsztzhigongyibaogongjizhinan---- 6
根据错误信息显示,代码中出现了NoAlertPresentException异常,即没有找到弹窗。这可能是因为在该网页上没有弹窗出现。
你可以尝试使用try-except语句来处理该异常,如下所示:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoAlertPresentException
driver = webdriver.Chrome()
driver.get("http://bj.bendibao.com/news/ztzhigongyibaogongjizhinan/")
try:
alert = driver.switch_to.alert
alert.accept()
except NoAlertPresentException:
pass
driver.find_element(By.XPATH, '//*[@id="savePicDom"]/div[2]/div[2]/div[3]/div[2]/div/div/div/div[1]/div').click()
这样,如果没有找到弹窗,就会捕获到NoAlertPresentException异常并忽略它,继续执行后续的代码
原文地址: https://www.cveoy.top/t/topic/iLIh 著作权归作者所有。请勿转载和采集!