关于弹出框点击确认不执行的问题

#切换至弹窗并点击确定
def accept_alert(self):
    try:
        WebDriverWait(self.driver, 20).until(EC.alert_is_present())
        alert = self.driver.switch_to.alert
        alert.accept()
    except BaseException:
         logger.error('弹出框失败.')

def receive_all_docs(self):
     self.click(*self.chkall_checkbox)
     self.click(*self.response_btn)
     self.accept_alert()

结果弹出框不出现,错误提示

raise exception_class(message, screen, stacktrace, alert_text) selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: ……? Message: Modal dialog present with text: ……    ?
讨论数量: 2
Jason990420

There must be somethig you didn't trace why it happened, and that's what you should provide for question here. If not, how people can help you to find the solution.

You can see following codes work ...

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome('D:/Python/Project/chromedriver')
driver.get('https://www.seleniumeasy.com/test/javascript-alert-box-demo.html')

print('No alert')

try:
    WebDriverWait(driver, 3).until(EC.alert_is_present())
    alert = driver.switch_to.alert
    alert.accept()
    print('OK')
except BaseException:
    print('弹出框失败.')

print('With Alert')
driver.find_elements_by_tag_name('button')[1].click()

try:
    WebDriverWait(driver, 3).until(EC.alert_is_present())
    alert = driver.switch_to.alert
    alert.accept()
    print('OK')
except BaseException:
    print('弹出框失败.')

driver.close()
driver.quit()
2周前 评论
joeyun (楼主) 2周前

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!