Python:循环中发生异常时代码未运行

Python:循环中发生异常时代码未运行,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我正在运行一个python代码,在for循环中进行for循环迭代,但是,如果抛出异常,则要在exception下执行的代码不会执行,并且代码在exception中无限循环,而不会移动到主循环 错误消息如下: selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed from unknown error: web view not found (

我正在运行一个python代码,在for循环中进行for循环迭代,但是,如果抛出异常,则要在exception下执行的代码不会执行,并且代码在exception中无限循环,而不会移动到主循环

错误消息如下:

selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found
  (Session info: chrome=73.0.3683.86)
  (Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 10.0.17763 x86_64)

我尝试过的代码:

范围内(100)的

打印(“主循环过程”)
对于fb_按钮中的按钮:
driver.switch\u to.window(driver.window\u句柄[1])
尝试:
虽然你是对的:
对于like_right中的right:
右键。单击()
打破
driver.switch\u to.window(driver.window\u句柄[0])
除了(NoSuchWindowException、ElementNotVisibleException、StaleElementReferenceException)作为e:
driver.switch\u to.window(driver.window\u句柄[0])
持续
除StaleElementReferenceException作为e外:
时间。睡眠(10)
刷新。单击()
其他:
时间。睡眠(5)
刷新。单击()
打印(“刷新”)

谷歌搜索/文档什么都没找到……我感到奇怪的是,selenium可以抛出一个异常,但却无法捕获它。

下面的
中断
使得下面的一行无法访问

break
driver.switch_to.window(driver.window_handles[0]) # <--- unreachable
如果没有
break
s,此代码将在第一次迭代中捕获两个异常。这里还有一个
for else
块。请注意,在第三次迭代之后,此代码将执行其他操作

for iteration in range(5):
    for element in sequence:
        try:
            while sequence:
                for character in element:
                    sequence.pop()
                    sequence[1].split() + 1
        except (NameError, TypeError, ValueError) as e:
            print(f"Caught first exception: {e}")
            run_after_type_error_exception()
            # break
        except IndexError as e:
            print(f"Caught exception {e}")
            run_after_index_error_exception()
            # break
    else:
        print("Do something else")
    print(f"Current iteration: {iteration}")

另外,请注意
time.sleep(以秒为单位)
因此,如果您的代码的行为与上述代码类似(在
else
部分~一半的时间内),那么您将
睡眠
至少4分钟…

您对发生的事情的描述非常混乱。此外,在第一个块中调用同一行代码似乎有点麻烦,因为第一个块可能导致异常。我不确定到底发生了什么,但有一件事我想知道的是,如果你有“继续”,也许你的意思是“休息”?-continue可以很好地解释为什么您没有“移动到主循环”。@Steve,我已经尝试在异常中添加break,但是异常下的代码仍然没有执行,基本上我正在尝试在异常发生时执行代码:下面是我更改的代码:
except(不包括WindowException、ElementNotVisibleException、StaleElementReferenceException)作为e:driver.switch_to.window(driver.window_句柄[0])刷新。单击()中断
for iteration in range(5):
    for element in sequence:
        try:
            while sequence:
                for character in element:
                    sequence.pop()
                    sequence[1].split() + 1
        except (NameError, TypeError, ValueError) as e:
            print(f"Caught first exception: {e}")
            run_after_type_error_exception()
            # break
        except IndexError as e:
            print(f"Caught exception {e}")
            run_after_index_error_exception()
            # break
    else:
        print("Do something else")
    print(f"Current iteration: {iteration}")