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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM