selenium webdriver 中 "Thread.sleep(5000)" 命令的替代方法

Alternative way for "Thread.sleep(5000)" command in selenium webdriver

我需要在对我的自动化测试用例执行每个操作后使用此 "Thread.sleep(5000)" 命令。 有没有其他方法可以在每次操作后跳过写入此命令。

如果我跳过此命令,我会收到 "time out" 和 "element not found" 错误。

您可以使用隐式等待。在初始化 WebDriver 后添加此行: (C# 中的示例)

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

引用自:

在每个动作后加上Thread.sleep(5000)绝对不是个好主意。如果您将进行大量测试,那么完成所有测试将花费很多时间(例如数小时)。

对于这些情况,WebDriver 中有 waiting 机制,它基本上同步代码和浏览器行为中的操作,因此您不会得到 ElementNotFoundException,因为所有操作都会同步。

您可以在 Selenium 文档上看到很多示例 http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp