Home > Back-end >  Repeat a faulty iteration in try block in "for element in" loop - python
Repeat a faulty iteration in try block in "for element in" loop - python

Time:01-07

I write a script in python using selenium.

Basically, i open a file of addresses, and try to locate the information layer associated with each address on the ArcGis site.

for element in AdressesList:

adress = element

    try:

        driver = webdriver.Chrome('D:\downloads\chromedriver_win32\chromedriver')
        driver.get("https://www.arcgis.com/home/webmap/viewer.html?layers=c9fa1db72f74433b8eea48ed82a4e4f9") 
        time.sleep(6) // All sorts of things are loaded in the website

        search_bar = driver.find_element_by_id("geocoder_input")
        search_bar.clear()
        search_bar.send_keys(adress)
        search_bar.send_keys(Keys.RETURN) //search address

        time.sleep(3) // All sorts of things are loaded in the website
        mouse.move(422, 516, True, 0.1) 
        mouse.click('left') // make the element show up

        stat_11 = driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[3]/div[5]/div[2]/div[1]/div[3]/div[3]/div[1]/div[2]/div/div/div[2]/div[3]/table/tr[2]/td[2]/span").text // The specific webelemnt xpath within which the information I need is stored 

        driver.close();
        driver.quit();

        print()

        LinesToTxtFile.append("NO"   ", "   adress.replace(',', '-')   ", "   str(stat_11))

    except:
        LinesToTxtFile.append("An, "   "exception, "   "occurred")

That's all the relevant part. What I want to do, and I have not found an answer to it, is to repeat iteration to a current element in case the code throws an exception.

That is,

// try block here

except:

LinesToTxtFile.append("An, "   "exception, "   "occurred")
RepeatCurrentIterationAgainWithTheSameElement()  // This is for illustration purposes only

Thank you!

CodePudding user response:

Add a while True and break when the code is done:

for element in AdressesList:

    adress = element

    while True:
        try:

            driver = webdriver.Chrome('D:\downloads\chromedriver_win32\chromedriver')
            driver.get("https://www.arcgis.com/home/webmap/viewer.html?layers=c9fa1db72f74433b8eea48ed82a4e4f9") 
            time.sleep(6) // All sorts of things are loaded in the website

            search_bar = driver.find_element_by_id("geocoder_input")
            search_bar.clear()
            search_bar.send_keys(adress)
            search_bar.send_keys(Keys.RETURN) //search address

            time.sleep(3) // All sorts of things are loaded in the website
            mouse.move(422, 516, True, 0.1) 
            mouse.click('left') // make the element show up

            stat_11 = driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[3]/div[5]/div[2]/div[1]/div[3]/div[3]/div[1]/div[2]/div/div/div[2]/div[3]/table/tr[2]/td[2]/span").text // The specific webelemnt xpath within which the information I need is stored 

            driver.close();
            driver.quit();

            print()

            LinesToTxtFile.append("NO"   ", "   adress.replace(',', '-')   ", "   str(stat_11))
            break

        except Exception:
            LinesToTxtFile.append("An, "   "exception, "   "occurred")
  •  Tags:  
  • Related