[英]Continue in Python loop not restarting after try and except
The loop does not restart after an exception occurs in a python loop. 在python循环中发生异常后,循环不会重新启动。 I have tried both
continue
and pass
. 我已经尝试过
continue
和pass
。 When i use continue
the loop does not proceed forward it is stuck at the exception. 当我使用
continue
,循环不会继续前进,它被卡在异常中。 when i use pass
it captures on the the id that has the error and the the ones before that even if there is no error when i print it shows as error. 当我使用
pass
它将捕获具有错误的id和之前的id,即使在我打印时没有错误,它也会显示为错误。
This is my code that i am using. 这是我正在使用的代码。
for i in ids:
try:
driver.find_element_by_xpath('//*[@id="print_area"]/table/tbody/tr[16]/td[1]/a').click()
driver.find_element_by_xpath('//*[@id="print_area"]/table/tbody/tr[4]/td[3]/a').click()
# searching for an id.
driver.find_element_by_xpath('//*[@id="ctl00_ctl00_cphMain_cphMain_txtEmprAcctNu"]').send_keys(i) driver.find_element_by_id('ctl00_ctl00_cphMain_cphMain_btnSearch').click()
driver.find_element_by_xpath('//*[@id="ctl00_ctl00_cphMain_cphMain_grdAgentEmprResults"]/tbody/tr[2]/td[1]/a').click()
#navigating to the profile
driver.find_element_by_xpath('//*[@id="print_area"]/table/tbody/tr[8]/td[3]/a').click()
driver.find_element_by_xpath('//*[@id="print_area"]/table/tbody/tr[4]/td[1]/a').click()
#copying the and storing the date
subdate = driver.find_element_by_id('ctl00_ctl00_cphMain_cphMain_frmViewAccountProfile_lblSubjectivityDate').text
subjectivitydate.append(subdate)
#exiting current details
driver.find_element_by_id('ctl00_ctl00_cphMain_ULinkButton4').click()
sleep(1)
except Exception as e:
continue
Any suggestions how do i restart the loop with the next id. 任何建议如何重新启动下一个ID的循环。 The exceptions occur either above or below the search bar.
例外发生在搜索栏的上方或下方。
Regards, Ren. 问候,任。
Example version: 示例版本:
ids = [1, 2, 3, 2]
other_id = [1, 2, 3]
id_generator = (id for id in ids) # this is a generator, needs the brackets
while True:
try:
id = next(id_generator)
except StopIteration:
break
try:
print(other_id[id])
except Exception as e: # this will happen when id = 3, and is not a valid index in other_id
continue
A version for your code 您的代码的版本
id_generator = (id for id in ids) # this is a generator, needs the brackets
while True
try:
id = next(id_generator)
except StopIteration:
break
try:
driver.find_element_by_xpath('//*[@id="print_area"]/table/tbody/tr[16]/td[1]/a').click()
driver.find_element_by_xpath('//*[@id="print_area"]/table/tbody/tr[4]/td[3]/a').click()
# searching for an id.
driver.find_element_by_xpath('//*[@id="ctl00_ctl00_cphMain_cphMain_txtEmprAcctNu"]').send_keys(id)
driver.find_element_by_id('ctl00_ctl00_cphMain_cphMain_btnSearch').click()
driver.find_element_by_xpath('//*[@id="ctl00_ctl00_cphMain_cphMain_grdAgentEmprResults"]/tbody/tr[2]/td[1]/a').click()
#navigating to the profile
driver.find_element_by_xpath('//*[@id="print_area"]/table/tbody/tr[8]/td[3]/a').click()
driver.find_element_by_xpath('//*[@id="print_area"]/table/tbody/tr[4]/td[1]/a').click()
#copying the and storing the date
subdate = driver.find_element_by_id('ctl00_ctl00_cphMain_cphMain_frmViewAccountProfile_lblSubjectivityDate').text
subjectivitydate.append(subdate)
#exiting current details
driver.find_element_by_id('ctl00_ctl00_cphMain_ULinkButton4').click()
sleep(1)
except Exception as e:
continue
Thanks @Jinglesting. 谢谢@Jinglesting。 I solved it by breaking the code into sub parts by using
if
and then in except
i redirected it to the home page. 我通过使用
if
and then将代码分为多个子部分来解决它, except
我将其重定向到了主页。
for i in ids:
driver.find_element_by_xpath('//*[@id="print_area"]/table/tbody/tr[16]/td[1]/a').click()
driver.find_element_by_xpath('//*[@id="print_area"]/table/tbody/tr[4]/td[3]/a').click()
- # searching for an id.
driver.find_element_by_xpath('//*[@id="ctl00_ctl00_cphMain_cphMain_txtEmprAcctNu"]').send_keys(i)
driver.find_element_by_id('ctl00_ctl00_cphMain_cphMain_btnSearch').click()
try:
if driver.find_element_by_xpath('//*[@id="ctl00_ctl00_cphMain_cphMain_grdAgentEmprResults"])./tbody/tr[2]/td[1]/a')!=0:
click()
driver.find_element_by_xpath('//*[@id="print_area"]/table/tbody/tr[8]/td[3]/a').click()
driver.find_element_by_xpath('//*[@id="print_area"]/table/tbody/tr[4]/td[1]/a').click()
subdate = driver.find_element_by_id('ctl00_ctl00_cphMain_cphMain_frmViewAccountProfile_lblSubjectivityDate').text
subjectivitydate.append(subdate)
driver.find_element_by_id('ctl00_ctl00_cphMain_ULinkButton4').click()
except NoSuchElementException as e:
driver.find_element_by_xpath('//*[@id="leftNavColumn"]//*[text()="Home"]').click()
continue
Thanks for all the help i learned something new about generators. 感谢您提供的所有帮助,我了解了一些有关发电机的新知识。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.