from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import UnexpectedAlertPresentException
#存在弹窗处理方法一 :
EC.alert_is_present()(driver)检测是否存在弹窗
try:
WebDriverWait(driver, 10).until(EC.title_is(u"我的门户"))
except UnexpectedAlertPresentException: #alert处理
print("alert处理")
while EC.alert_is_present()(driver): #循环检测,可应对数量不定弹窗
result = EC.alert_is_present()(driver)
print(result.text)
result.accept()
print(‘登录失败,再次登录‘)
login()
except exceptions.TimeoutException: #20191215
login() #登录失败,再次登录
else: #通过登录
print("通过登录")
#存在弹窗处理方法二 :
print("alert处理")
try:
for i in range(2): #可应对可能出现一个或二个弹窗
alert = driver.switch_to.alert
print(alert.text)
alert.accept() #去除浏览器警告
except NoAlertPresentException:
pass
#‘‘‘弹窗处理方法三,示例代码
try:
WebDriverWait(driver, 10, 0.5).until(EC.alert_is_present())
alert = driver.switch_to.alert
print(alert.text)
alert.accept() #去除浏览器警告
except exceptions.TimeoutException:
print("no alert")
原文:https://www.cnblogs.com/thyuan/p/12043794.html
本文介绍了使用Selenium与Python处理浏览器弹出框的方法,包括使用`EC.alert_is_present()`检测并接受弹窗,循环检测处理不确定数量的弹窗,以及通过循环尝试处理可能存在的多个弹窗情况。
925

被折叠的 条评论
为什么被折叠?



