6

I have the following code:

WebDriverWait(self.driver, 20).until(expected_conditions.element_to_be_clickable(click))

Now this sometimes fails and I know why it fails. But the error gives me

TimeoutException: Message: 

Which is useless. Can I possibly set this message?

Andrew
  • 1,571
  • 17
  • 31

3 Answers3

7

You don't have to try/except or wrap anything; message is just an argument of the until() method.

WebDriverWait(self.driver, 20).until(
    expected_conditions.element_to_be_clickable(click),
    message='My custom message.',
)

This produces:

selenium.common.exceptions.TimeoutException: Message: My custom message.
florisla
  • 12,668
  • 6
  • 40
  • 47
0

A simple try except block should do the job?

try:
    WebDriverWait(self.driver, 20).until(expected_conditions.element_to_be_clickable(click))
except TimeoutException:
    print("Something")
    #or do anything else like self.browser.close()
    print (traceback.format_exc())

And if you'd wanted to edit the Message itself that would be another thing, you'd have to create a custom Error message in the Python for this case or create a custom exception. Hopefully, that's something you're looking for.

innicoder
  • 2,612
  • 3
  • 14
  • 29
  • I thought they must allow some way of setting the error message. It seems silly to send an empty string all the time. – Andrew May 11 '17 at 19:11
  • Haha, it is silly because the thing is they didn't account for this type of Error to occur at all. That's why you have to either add this particular case to the Exception handling ( Which I've never done) or create a custom exception (there are a lot of youtube and other types of documentation for this). Maybe someone else has a solution. – innicoder May 11 '17 at 19:13
  • 3
    Ugh. That depressing moment when the Google result you find is your own question on SO. – Andrew Jul 19 '17 at 19:41
0

I wrote a stupid wrapper class. This will always output "Timed out on wait" instead of blank text. If you want more specific text, you have to create a wrapper class or a new wait class that applies the function "get_error". I've included my jquery animation wait example at the bottom.

'''
Wrapper for WebDriverWait
'''

from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException

class Wait(WebDriverWait):
    def __init__(self, driver, timeout):
        self.driver = driver
        self.timeout = timeout
        self.wait = WebDriverWait(driver, timeout)

    def until(self, condition):
        try:
            self.wait.until(condition)
        except TimeoutException as exception:
            error_func = getattr(condition, "get_error", None)
            if callable(error_func):
                raise TimeoutException(error_func())
            else:
                raise TimeoutException("Timed out on wait")

    def until_not(self, condition):
        try:
            self.wait.until_not(condition)
        except TimeoutException as exception:
            error_func = getattr(condition, "get_error", None)
            if callable(error_func):
                raise TimeoutException(error_func())
            else:
                raise TimeoutException("Timed out on wait")

WaitForAnimation class:

'''
Wait for a jquery animation to finish
'''

from selenium.webdriver.support import expected_conditions

class WaitForAnimation(object):
    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
        return not driver.execute_script("return jQuery('"+self.locator+"').is(':animated')")

    def get_error(self):
        return "Timed out waiting for animation of " + self.locator
Andrew
  • 1,571
  • 17
  • 31