Yogwhatup
Yogwhatup

Reputation: 362

retry where script/program left off in python remembering variable value

I would like my script to re-try where it left off when an error occurs. I am using selenium to download multiple reports. Once in a while a report will not load correctly. I want the script, on error, to remember where it was and re-try to pull the report. (I need the script to remember the value of x).

Here is some code that might help...(I desperately need help)

import time
import os
import glob
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import MoveTargetOutOfBoundsException
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException

for x in range(1, 51):      
    while True:    
        try:
            fp = webdriver.FirefoxProfile('C:/Users//Documents/FirefoxProfile')
            browser = webdriver.Firefox(fp)
            browser.get('https://reportlocation/')

            time.sleep(8)

            browser.find_element_by_id("ctl00_PlaceHolderMain_login_UserName").clear()
            browser.find_element_by_id("ctl00_PlaceHolderMain_login_UserName").send_keys("ytb971")
            browser.find_element_by_id("ctl00_PlaceHolderMain_login_password").clear()
            browser.find_element_by_id("ctl00_PlaceHolderMain_login_password").send_keys("Fender25")
            browser.find_element_by_id("ctl00_PlaceHolderMain_login_login").click()

#gets user to reporting front end

            ReportMgr= browser.find_element_by_partial_link_text('Report Manager')
            ReportMgr.click()

            time.sleep(5)

            CustomReport= browser.find_element_by_partial_link_text('Custom Report')
            CustomReport.click()

            time.sleep(5)

            ProgramManagement= browser.find_element_by_partial_link_text('Program Management')
            ProgramManagement.click()
            ProgramManagement= browser.find_element_by_partial_link_text('Program Management').send_keys(Keys.ARROW_LEFT)

#pulls reports

            browser.find_element_by_partial_link_text('Program Management').click()
            time.sleep(10)
            browser.find_element_by_partial_link_text('Program Management').send_keys(Keys.ARROW_DOWN * x, Keys.ENTER, Keys.ENTER)
            time.sleep(30)
            browser.find_element_by_css_selector("#ctl00_PlaceHolderMain_ReportViewer1_HtmlOutputReportResults2_CSVButton_ImageAnchor > img").click()
            time.sleep(10)
            browser.find_element_by_partial_link_text('Program Management').click()
            time.sleep(10)
            browser.quit()

            except:
                continue
            else:
                break

Upvotes: 0

Views: 274

Answers (2)

John Gordon
John Gordon

Reputation: 33345

You could write the current value of x to a file in the current directory, and then remove the file when the script is done. When the script starts up, if the file is still there, then you know that there was an error because the file wasn't removed. So, read the file to get x and start from there.

But overall, a better solution would be to structure your program so that the while True loop is above the for x in range loop, so breaking out of one report will naturally proceed on to the next report, instead of skipping all the remaining ones.

Upvotes: 0

Thomas Lotze
Thomas Lotze

Reputation: 5323

What you do here is iterate over 50 values of x, and as soon as one access fails, you recursively start iterating over all 50 values again. The repeat logic should rather look like this:

for x in range(1, 51):
    while True:  # or some condition to put a bound on retries
        try:
            ...
        except:
            continue
        else:
            break
therest()

This way, you catch each exception still inside the loop over the 50 values, and you perform retries within your running iteration so remembering the current value of x becomes a non-issue. Note the use of else with the try statement: the else clause is executed if no exception occurred; this is when you want to stop retrying with the current value of x.

Upvotes: 1

Related Questions