I'm writting some scripts on Selenium combined with Saucelab. However I have an issue. I do a test condition to know if I have an element or not on my page, if there is not this element I want to stop the test and send a fail flag to Saucelab.

My code is:

    if (len(driver.find_elements_by_xpath("//div[@data-user-id='34196'][@data-shift-date='%s']/div/div[@class='shift-details']" %date))!=0 ):
    print "The shift is assigned to this user, good"
    print "The shift is not assigned to this user, issue"
    sauce_client.jobs.update_job(driver.session_id, passed=False)

sauce_client.jobs.update_job(driver.session_id, passed=True)

My problem is that driver.quit() in the else part doesn't work as I would like. The script is still running.

  • Do you get an error if the element is not found? Does it quit when it is found? Commented Feb 10, 2017 at 16:45
  • When it doesn't found the element, I see "The shift is not assigned..." on my console but the quit command seems to be ignored
    – LeBrequin
    Commented Feb 10, 2017 at 16:53

1 Answer 1


If the element cannot be found the driver throws an exception. Then the program stops. You need to handle the exception with a try ... except block.

Read how to handle errors: https://docs.python.org/3/tutorial/errors.html


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.