【发布时间】:2019-08-21 21:28:40
【问题描述】:
我有一个嵌套的while 语句,在第二个语句中我有一个try 块。在except 块中,我有一些代码,然后是continue。问题是当它遇到continue 时,它将从first while 开始,而不是预期的嵌套while。
我一直在寻找,但我不确定这是否是预期的行为?
while iRowCount < iMaxRows:
# ...
# Code here for getting information from a database
# ...
print("Iterating")
while i2RowCount < i2MaxRows:
try:
readUrl = "someURL" # This is assigned from the database
BrowserObj.get(readUrl) # Using Webdriver to navigate to the URL
elem = BrowserObj.find_element_by_link_text("Find Me")
elemHref = elem.get_attribute('href')
BrowserObj.get(elemHref) # Navigate to new link found
# This is never reached
theOutput = BrowserObj.find_element_by_css_selector(".classNames.here")
print("Out: " + str(theOutput))
exit()
except NoSuchElementException as E:
print("Not found. (Iteration: #" + str(i2RowCount) + ")")
i2RowCount += 1
continue
i2RowCount += 1
iRowCount += 1
异常将在elem = BrowserObj.find_element_by_link_text("Find Me") 处引发。但我的输出将如下:
Iterating
Not found. (Iteration: #0)
Iterating
Not found. (Iteration: #0)
Iterating
Not found. (Iteration: #0)
Iterating
Not found. (Iteration: #0)
Iterating
Not found. (Iteration: #0)
...this will keep going.
因此,由于某种原因,continue 似乎回到了我的第一个循环,而不是我的嵌套循环......我在这里做错了吗?
【问题讨论】:
-
欢迎来到 StackOverflow。请按照您创建此帐户时的建议阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布 MCVE 代码并准确说明问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中并重现您指定的问题。您发布的代码有几个未定义的符号,并且缺少设置代码。
标签: python windows loops iteration continue