Resetting loop when going to next page

Issue

I have the following code, that downloads files:

followLoop = range(1, 49)
for x in followLoop:

    try:

        xpath = '//*[@id="ctl00_CPH_Main_ctl00_RadGridFacturen_ctl00__'
        xpath += str(x)
        xpath += '"]/td[10]/input'

        time.sleep(2)

        driver.find_element_by_xpath(xpath).click()

    except NoSuchElementException:
            print('exception')
            nextPage = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_RadGridFacturen_ctl00"]/tfoot/tr/td/table/tbody/tr/td/div[3]/input[1]')
            nextPage.click()
            continue

When it throws an exception, I want the loop range to reset on the next page. So when page 1 throws an exception, I want in to start on page 2 with 1.

Does anyone know how I can achieve this?

Solution

You can use a while loop like this.

count = 1
while count < 49:
   try:
        xpath = '//*[@id="ctl00_CPH_Main_ctl00_RadGridFacturen_ctl00__' + str(x) + '"]/td[10]/input'
        time.sleep(2)
        driver.find_element_by_xpath(xpath).click()

        
   except NoSuchElementException:
        print('exception')
        nextPage = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_RadGridFacturen_ctl00"]/tfoot/tr/td/table/tbody/tr/td/div[3]/input[1]')
        nextPage.click()
        count = 1

This will work?

Answered By – Dosbodoke

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published