微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Selenium+python3 应对多个弹出框存在(alert_is_present)判断和处理

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")





版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐