-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 code elem.click() gets gives an error sometimes. If it does, I need to call elem = 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.

el323
  • 2,760
  • 10
  • 45
  • 80
  • See this answer for exception handling syntax: http://stackoverflow.com/questions/730764/try-except-in-python-how-do-you-properly-ignore-exceptions/2390566#2390566 – cbare Aug 21 '16 at 01:50
  • Possible duplicate of [In Python try until no error](http://stackoverflow.com/questions/4606919/in-python-try-until-no-error) – zondo Aug 21 '16 at 01:51

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
  • elem.click() needs to run several time before it stops throwing an exception. How can I handle this situation? – el323 Aug 21 '16 at 01:58
  • 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 code` `break` - sorry still getting use to formatting comments – Caleb McIvor Aug 21 '16 at 02:13
  • I will add it to my answer – Caleb McIvor Aug 21 '16 at 02:22
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 ... 
hossein
  • 313
  • 1
  • 8
  • But wouldn't that try block execute just once? I need to call elem.click() until it executes and does not gives an error. – el323 Aug 21 '16 at 01:53
  • You can use this code in a loop. You only need to find good control flow for it. Maybe retry until reaches max retry. – hossein Aug 21 '16 at 01:59
0

generic way to handle exception in python is

try:
    1/0
except 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_xpath

if ( 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

0

Look at try and except

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.