继续在Python循环中尝试除之后不重新启动

时间:2018-03-28 13:42:45

标签: python

在python循环中发生异常后,循环不会重新启动。我尝试了continuepass。当我使用continue时,循环不会继续前进,它会停留在异常处。当我使用pass时,它会捕获有错误的id和之前的错误,即使我打印时没有错误也显示为错误。

这是我正在使用的代码。

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

任何建议我如何使用下一个id重新启动循环。例外情况发生在搜索栏的上方或下方。

此致 任

2 个答案:

答案 0 :(得分:0)

示例版本:

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

代码的版本

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

答案 1 :(得分:0)

谢谢@Jinglesting。我通过使用if将代码分解为子部分来解决它,然后在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

感谢所有帮助,我学到了关于发电机的新知识。