Python实现 等待条件成立或超时后返回

最近用robotframework 自带的selenium库关键字进行页面脚本编写测试,发现有很多等待关键字,等待条件成立或时间结束后返回,本人之前一直在思考等待命令执行一定时间在接着执行,
认为可以借鉴次代码,将此处代码复制粘贴在博客上。

   def _wait_until_worker(self, condition, timeout, error):
        max_time = time.time() + timeout
        not_found = None
        while time.time() < max_time:
            try:
                if condition():
                    return
            except ElementNotFound as err:
                not_found = str(err)
            else:
                not_found = None
            time.sleep(0.2)
        raise AssertionError(not_found or error)
原文地址:https://www.cnblogs.com/Finding-bugs/p/14266569.html