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

北战南征 提交于 2021-02-19 02:47:10

问题


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?


回答1:


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.



回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/43922933/in-python-selenium-how-do-i-set-the-error-messages-for-a-wait

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!