-1
elem = browser.find_element_by_xpath(".//label[@class = 'checkbox' and contains(.,'Últimos 15 días')]/input")if ( elem.is_selected() ):    print "already selected"else:    elem.click()

In my codeelem.click() gets gives an error sometimes. If it does, I need to callelem = browser.find_element_by_xpath again i.e. the first line of the code.

Is there a way to achieve this using exception handling in python.Help will be much appreciated.

askedAug 21, 2016 at 1:44
el323's user avatar
2

4 Answers4

2

From what I can understand this can be done with exception handling.you could try the following:

elem = browser.find_element_by_xpath(".//label[@class = 'checkbox' and contains(.,'Últimos 15 días')]/input")if ( elem.is_selected() ):    print "already selected"else:    while True:        try:            #code to try to run that might cause an error            elem.click()         except Exception:            #code to run if it fails            browser.find_element_by_xpath        else:            #code to run if it is the try is successful            break        finally:             #code to run regardless
answeredAug 21, 2016 at 1:54
Caleb McIvor's user avatar
Sign up to request clarification or add additional context in comments.

3 Comments

elem.click() needs to run several time before it stops throwing an exception. How can I handle this situation?
Add the whole try block into a while loop and break it only when it is successful eg.while True:try: ` #command`except Exception:#error codebreak - sorry still getting use to formatting comments
I will add it to my answer
0

You need try/except statement there.

try:  elem.click()except Exception: # you need to find out which exception type is raised  pass  # do somthing else ...
answeredAug 21, 2016 at 1:49
hossein's user avatar

2 Comments

But wouldn't that try block execute just once? I need to call elem.click() until it executes and does not gives an error.
You can use this code in a loop. You only need to find good control flow for it. Maybe retry until reaches max retry.
0

generic way to handle exception in python is

try:    1/0except Exception, e:    print e

So in your case it would give

try:    elem = browser.find_element_by_xpath(".//label[@class =     'checkbox' and contains(.,'Últimos 15 días')]/input")except Exception, e:    elem = browser.find_element_by_xpathif ( elem.is_selected() ):    print "already selected"else:    elem.click()

It is better to use the more specific exception type. If you use the generic Exception class, you might catch other exception where you want a different handling

answeredAug 21, 2016 at 1:51
Vertical Jo's user avatar

Comments

0

Look attry andexcept

while elem == None:    try:        elem = browser.find_element_by_xpath(".//label[@class = 'checkbox' and contains(.,'Últimos 15 días')]/input")        if ( elem.is_selected() ):            print "already selected"        else:            elem.click()    except Exception, e:        elem = None

Obviously using the specific exception that is raised from the click.

answeredAug 21, 2016 at 1:52
Hayden Mack's user avatar

1 Comment

Edited to include the while loop, to loop until there is no error

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.