首页 > 其他 > 详细

try except循环3次失败抛异常

时间:2021-06-07 16:28:54      阅读:25      评论:0      收藏:0      [点我收藏+]

需求:

自动化点检定位元素。如果元素不存在,则刷新网页,刷新3次还定位到元素,则抛异常

 

原来

# 点击操作
def click_btn(by, locator):
    try:
        element = WebDriverWait(driver, 30).until(expected_conditions.element_to_be_clickable((by, locator)))
        element.click()
    except Exception as e:
        raise e

改版后

# 加强版点击操作(定位不到元素自动刷新3次)     
def click_btn(by, locator):
    try:
        for x in range(4):
            try:
                element = WebDriverWait(driver, 30).until(expected_conditions.element_to_be_clickable((by, locator)))
                element.click()
            except Exception as e:
                if x == 3:
                    raise e
                driver.refresh()
            else:
                break            
    except Exception as e:
        raise e

 

try except循环3次失败抛异常

原文:https://www.cnblogs.com/soymilk2019/p/14858957.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!