Python global error handling

0

I want to know if there is any way to handle any type of error that appears in the browser so that within my function Connections (url) any object not found by the ccs_selector instead of cutting the program back to run from the beginning def connections.

I had thought something like this:

try:
    a = 12
    b = 13
    c = 14
except:
    run(a)

I know that there is not that what I wrote but it is a way to graph what I am looking for. If for any reason I can not generate a = 12, b = 13, c = 14 try again! In the case of my code would be serious if any action within def could not be specified re-execute def

def Connections(url):
    df = pd.read_excel(">0.20 fillrate.xlsx", header = None,  converters={0: str}).pivot(columns = 0,  values = 1)
    Lista = {col: df[col].dropna().tolist() for col in df.columns}
    for id,  tags in Lista.iteritems():
        urlcn = driver.get(url.format(id.strip()))
        driver.refresh()
        try:
            element_present = EC.presence_of_element_located((By.CSS_SELECTOR, ".navbar-inhiner > ul:nth-child(1) > li:nth-child(4) > a:nth-child(1)"))
            WebDriverWait(driver, timeout).until(element_present)
    except TimeoutException:
        print ("Timed out waiting for page to load")
    driver.find_element_by_css_selector('.navbar-inhiner > ul:nth-child(1) > li:nth-child(4) > a:nth-child(1)').click()
    try:
        element_present = EC.presence_of_element_located((By.CSS_SELECTOR, "li.ng-scope:nth-child(6) > a:nth-child(1)"))
        WebDriverWait(driver, timeout).until(element_present)

    except TimeoutException:
        print ("Timed out waiting for page to load")    
    driver.find_element_by_css_selector('li.ng-scope:nth-child(6) > a:nth-child(1)').click()
    #Cargado de tags
    try:
        element_present = EC.presence_of_element_located((By.CSS_SELECTOR, "span.ng-scope:nth-child(12) > div:nth-child(1) > span:nth-child(1)"))
        WebDriverWait(driver, timeout).until(element_present)

    except TimeoutException:
        print ("Timed out waiting for page to load")
    driver.find_element_by_css_selector('span.ng-scope:nth-child(12) > div:nth-child(1) > span:nth-child(1)').click()
    try:
        element_present = EC.presence_of_element_located((By.CSS_SELECTOR, "#s2id_autogen30"))
        WebDriverWait(driver, timeout).until(element_present)

    except TimeoutException:
        print ("Timed out waiting for page to load")    
    nombre = driver.find_element_by_css_selector('#s2id_autogen30')
    for tag in tags:
        nombre.send_keys(tag)
        try:
            element_present = EC.presence_of_element_located((By.CSS_SELECTOR, '.select2-results-dept-0'))
            WebDriverWait(driver, timeout).until(element_present)
        except TimeoutException:
                print ("Timed out waiting for page to load")
        try:
            nombre2 = driver.find_element_by_css_selector('.select2-results-dept-0').click()
        except:
            pass
            nombre.clear()
        try:
            driver.find_element_by_css_selector('button.btn-success:nth-child(3)').click()
        except:
            pass
    try:
        element_present = EC.presence_of_element_located((By.CSS_SELECTOR, "div.modal-footer:nth-child(3) > button:nth-child(1)"))
        WebDriverWait(driver, timeout).until(element_present)

    except TimeoutException:
        print ("Timed out waiting for page to load")
    driver.find_element_by_css_selector('div.modal-footer:nth-child(3) > button:nth-child(1)').click()
    #Guardado de la connection
    try:
        element_present = EC.presence_of_element_located((By.CSS_SELECTOR, ".bs-docs-social-buttons > li:nth-child(2) > button:nth-child(1)"))
        WebDriverWait(driver, timeout).until(element_present)
    except TimeoutException:
        print ("Timed out waiting for page to load")
    d= driver.current_url
    time.sleep(5)
    driver.find_element_by_css_selector(".bs-docs-social-buttons > li:nth-child(2) > button:nth-child(1)").click()
    element_present = EC.presence_of_element_located((By.CSS_SELECTOR, "adap-marketplace-connections-grid-filter.ng-scope > button:nth-child(1)"))
    WebDriverWait(driver, timeout).until(element_present)
    print(d)
    time.sleep(5)
    driver.refresh()
    time.sleep(5)
    
asked by Martin Bouhier 01.11.2017 в 22:43
source

1 answer

1

You can use by default

try:
    int("y")
except Exception as error:
    print("Ha ocurrido un error: ", repr(error))
    run(10)

It is good to print the error to know what is wrong.

And what you wrote if you can do it can even be combined with a retry, for example:

for i in range(5):
    try:
        kilometros = calcular_kilomentros(puntos)
        if kilometros != 0:
            break
    except Exception as error:
        print (" Error : ", repr(error))

This code retries 5 times, if you do it the first time it just goes on.

For example if what you want is that when you fail for any mistake, try up to 5 times you should do this.

def Connections(url): 
    variables = [10,11,13,14,15]
    cont = 0
    for i in range(5):
        try:
            # aqui ira todo tu código 
            #ejemplo 
            run(variables[cont])
            cont +=1   # con esto si por ejemplo falla cuando vas ejecutando run(11), cuando reintente ejecutara run(variables[cont]), que sería igual a run(11) otra vez, es decir continua donde iba 
            break  #para que termine si no hay errores
        except Exception as error:
            print (" Error : ", repr(error))

To make it work, you should not capture the errors with all the try that you have, unless they are necessary.

I hope it serves you. greetings

    
answered by 01.11.2017 / 22:46
source