In python, selenium, how do I set the error messages for a wait?
Asked Answered
H

3

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?

Housework answered 11/5, 2017 at 18:19 Comment(0)
S
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.
Stool answered 31/10, 2017 at 7:56 Comment(1)
That's interesting. I see that parameter in the doc, but it doesn't say what it does.Housework
A
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.

Astroid answered 11/5, 2017 at 19:5 Comment(3)
I thought they must allow some way of setting the error message. It seems silly to send an empty string all the time.Housework
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.Astroid
Ugh. That depressing moment when the Google result you find is your own question on SO.Housework
H
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
Housework answered 19/7, 2017 at 22:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.