Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 出现错误或异常后需要重新运行脚本_Python_Python 2.7_Selenium_Selenium Webdriver - Fatal编程技术网

Python 出现错误或异常后需要重新运行脚本

Python 出现错误或异常后需要重新运行脚本,python,python-2.7,selenium,selenium-webdriver,Python,Python 2.7,Selenium,Selenium Webdriver,在出现“列表索引超出范围”错误或页面未加载并出现超时导致的错误或异常后,需要重新运行selenium python脚本。不,增加页面加载时间没有帮助,因为它会不断尝试加载。那么,如果脚本遇到问题,有没有办法让它自行停止并重新启动?像这样 while True: try: <your script> except <error>: pass else: break @凯文:你说得对。但这个问题不需要更

在出现“列表索引超出范围”错误或页面未加载并出现超时导致的错误或异常后,需要重新运行selenium python脚本。不,增加页面加载时间没有帮助,因为它会不断尝试加载。那么,如果脚本遇到问题,有没有办法让它自行停止并重新启动?

像这样

while True:
    try:
        <your script>
    except <error>:
        pass
    else:
        break

@凯文:你说得对。但这个问题不需要更多

def initiate_test_condition():
    """
    This should have the initial setup (Environment where your testcase to be tested)
    Example:
    * reopening your browser and 
    * kill all already opened browsers
    """
    pass

def test_case_flow():
    """
    write your selenium testing code
    """
    initiate_test_condition()
    try:
        """
        your testing code
        """     
        # if all code runs successfully
        return 1
    except Exception as error:
        print str(error)
        return 0


if __name__ == '__main__':
    total_rerun = 0
    while True:
        if total_rerun != 5 and total_rerun < 5:
            if test_case_flow():
                print "Looks like the code is passed :)"
                return or break
            else:
                total_rerun += 1
                print "Rerunning the code"
        else:
            print "Code Failed after rerunning %d times "  % total_rerun
            break