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





猜你喜欢

转载自www.cnblogs.com/thyuan/p/12043794.html