在python、selenium中,如何设置等待的错误消息?

And*_*rew 6 python selenium timeoutexception

我有以下代码:

WebDriverWait(self.driver, 20).until(expected_conditions.element_to_be_clickable(click))
Run Code Online (Sandbox Code Playgroud)

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

TimeoutException: Message: 
Run Code Online (Sandbox Code Playgroud)

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

flo*_*sla 7

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

WebDriverWait(self.driver, 20).until(
    expected_conditions.element_to_be_clickable(click),
    message='My custom message.',
)
Run Code Online (Sandbox Code Playgroud)

这会产生:

selenium.common.exceptions.TimeoutException: Message: My custom message.
Run Code Online (Sandbox Code Playgroud)


Yon*_*ono 0

一个简单的 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())
Run Code Online (Sandbox Code Playgroud)

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

  • 啊。当你找到的谷歌结果是你自己的问题时,那令人沮丧的时刻。 (3认同)