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

时间:2015-09-15 13:02:31

标签: python python-2.7 selenium selenium-webdriver

在列表索引超出范围之后需要重新运行我的selenium python脚本'错误或页面没有加载,并通过一些错误或异常,因为它超时。不,增加页面加载的时间没有帮助,因为它会不断尝试加载。那么有没有办法让脚本在遇到问题时自行停止并重启?

2 个答案:

答案 0 :(得分:0)

喜欢这个吗?

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

答案 1 :(得分:0)

@kevin你是对的。但这个问题需要更多。

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