Reputation: 1971
Set-up
I have a list of urls which each contain a form. I use Selenium to fill the form and I loop over the urls. I.e.
for url in urls:
browser = webdriver.Chrome()
browser.implicitly_wait(30)
browser.get(url)
data = {} # dictionary containing variables to be inserted in the url's form
var1 = browser.find_element_by_id("id")
var1.clear()
var1.send_keys(data['var1'])
# here follow more variables to be inserted
where urls = [] # list containing all urls
. This works fine.
Problem
Every now and then I receive an unexpected error for one of the urls. For example, the error results from that particular url not having a specific field.
I adjust the code to be able to handle all urls missing that specific field. Everything is fine.
But, I need to restart the loop from the beginning – not efficient.
Is there a way to tell Python to restart the loop from the url which resulted in an error, instead of from the first url in the list?
Upvotes: 1
Views: 1584
Reputation: 86
Instead of having to tell python to start from that point, rather use 'try' 'except'. This will simply skip the url that is breaking your loop, and continue until it has looped over all the urls. You could also include a print statement to identify which url didn't work, and then go back to it afterwards
So,
try:
for url in urls:
browser = webdriver.Chrome()
browser.implicitly_wait(30)
browser.get(url)
data = {} # dictionary containing variables to be inserted in the url's form
var1 = browser.find_element_by_id("id")
var1.clear()
var1.send_keys(data['var1'])
except Exception as e:
print(url)
print('Exception:',e)
pass
Upvotes: 1
Reputation: 72
I guess you are debugging your code and you need to run your code from error out url.
as every one suggested try
except
block can be used to handle errors. But for your debug purpose, below are tweaks
i = 0 # for first time. next time you can assign it to index of error generating url
while i < len(urls):
try:
url = urls(i)
browser = webdriver.Chrome()
browser.implicitly_wait(30)
browser.get(url)
data = {} # dictionary containing variables to be inserted in the url's form
var1 = browser.find_element_by_id("id")
var1.clear()
var1.send_keys(data['var1'])
except:
print i
raise
You can debug you code from error out url and not from beginning of list.
Upvotes: 0
Reputation: 20214
You can use a while
and try/except
:
suppose your function return True
:
for url in urls:
success = False
while not success:
try:
data, success = your_function()
except:
success = False
Then you can just retry until it succeed.
The core idea is that you do not need to restart current for loop, but you can wrap your function in an internal while loop.
Upvotes: 0
Reputation: 302
If you use try except else
, it could be as following:
for url in urls:
browser = webdriver.Chrome()
browser.implicitly_wait(30)
browser.get(url)
data = {} # dictionary containing variables to be inserted in the url's form
try:
var1 = browser.find_element_by_id("id")
var1.clear()
var1.send_keys(data['var1'])
except Exception, exception:
print exception # will print the error but ignore the lines of the else statement
# or do something about the error
else:
print 'went well'
# here follow more variables to be inserted
# or continue your code
Upvotes: 0
Reputation: 6475
You can use continue
in the exception handle part of the code.
for url in urls:
try:
code may have exception
except:
continue
Upvotes: 0