【问题标题】:In python, selenium, how do I set the error messages for a wait?在 python、selenium 中,如何设置等待的错误消息?
【发布时间】:2017-05-11 18:19:48
【问题描述】:

我有以下代码:

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

现在这有时会失败,我知道为什么会失败。但是错误给了我

TimeoutException: Message: 

这是没用的。我可以设置此消息吗?

【问题讨论】:

    标签: python selenium timeoutexception


    【解决方案1】:

    您不必尝试/排除或包装任何东西; message 只是 until() 方法的一个参数。

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

    这会产生:

    selenium.common.exceptions.TimeoutException: Message: My custom message.
    

    【讨论】:

    • 这很有趣。我在文档中看到了该参数,但没有说明它的作用。
    【解决方案2】:

    一个简单的 try except 块就可以完成这项工作?

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

    如果您想编辑消息本身,那将是另一回事,您必须在 Python 中为这种情况创建自定义错误消息或创建自定义异常。 希望这是您正在寻找的东西。

    【讨论】:

    • 我认为他们必须允许以某种方式设置错误消息。一直发送一个空字符串似乎很愚蠢。
    • 哈哈,这很愚蠢,因为他们根本没有考虑到会发生这种类型的错误。这就是为什么您必须将此特殊情况添加到异常处理(我从未做过)或创建自定义异常(有很多 youtube 和其他类型的文档)。也许其他人有解决方案。
    • 呃。当您找到的 Google 结果是您自己对 SO 提出的问题时,那个令人沮丧的时刻。
    【解决方案3】:

    我写了一个愚蠢的包装类。这将始终输出“等待超时”而不是空白文本。如果您想要更具体的文本,您必须创建一个包装类或应用函数“get_error”的新等待类。我在底部包含了我的 jquery 动画等待示例。

    '''
    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 类:

    '''
    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-11
      • 1970-01-01
      • 2020-10-28
      • 2023-01-15
      • 2023-04-05
      • 2020-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多