Selenium 隐式等待总是占用整个等待时间还是可以更快完成?

dja*_*fan 3 selenium selenium-webdriver

Selenium 隐式等待总是占用整个等待时间还是可以更快完成?如果我将隐式等待设置为 10 秒,对 .findElement 的调用可以在几秒钟内完成还是总是需要整个 10 秒?

此页面暗示它等待整整 10 秒,这非常令人困惑,因为它不是 javadoc 所暗示的。

来自 WebDriver.java 的以下代码注释意味着它可以比隐式超时定义的更早完成的轮询操作。但是,评论中的最后一句话确实打破了这种信念,让我对此并不完全确定。如果它实际上是轮询,那么它将如何“对测试时间产生不利影响”,因为它不会在整个隐式等待持续时间内完成?

/**
 *  from WebDriver.java
 * Specifies the amount of time the driver should wait when searching for an element if
 * it is not immediately present.
 * <p/>
 * When searching for a single element, the driver should poll the page until the 
 * element has been found, or this timeout expires before throwing a 
 * {@link NoSuchElementException}. When searching for multiple elements, the driver 
 * should poll the page until at least one element has been found or this timeout has
 * expired.
 * <p/>
 * Increasing the implicit wait timeout should be used judiciously as it will have an 
 * adverse effect on test run time, especially when used with slower location 
 * strategies like XPath.
 * 
 * @param time The amount of time to wait.
 * @param unit The unit of measure for {@code time}.
 * @return A self reference.
 */
Timeouts implicitlyWait(long time, TimeUnit unit);
Run Code Online (Sandbox Code Playgroud)

另外,是否有人可以提供有关默认“轮询”发生频率的信息?

Ant*_*t's 6

一旦它能够找到元素,它就可以完成。如果不是,它会抛出错误并停止。轮询时间再次非常特定于驱动程序实现(不是 Java 绑定,而是驱动程序部分,例如:FireFox 扩展、Safari 扩展等)

正如我在这里提到的,这些是非常特定于驱动程序实现的。所有与驱动程序相关的调用都通过execute方法进行。

我正在介绍该execute方法的要点(您可以在此处找到完整的源代码):

protected Response execute(String driverCommand, Map<String, ?> parameters) {
    Command command = new Command(sessionId, driverCommand, parameters);
    Response response;

    long start = System.currentTimeMillis();
    String currentName = Thread.currentThread().getName();
    Thread.currentThread().setName(
        String.format("Forwarding %s on session %s to remote", driverCommand, sessionId));
    try {
      log(sessionId, command.getName(), command, When.BEFORE);
      response = executor.execute(command);
      log(sessionId, command.getName(), command, When.AFTER);

      if (response == null) {
        return null;
      }
      //other codes 
}
Run Code Online (Sandbox Code Playgroud)

线路:

response = executor.execute(command);
Run Code Online (Sandbox Code Playgroud)

说整个故事。executor是 类型CommandExecutor,所以所有调用都转到特定的驱动程序类,如ChromeCommandExecutor,SafariDriverCommandExecutor,它有自己的处理。

所以轮询取决于驱动程序的实现。

如果您想指定轮询时间,那么您可能应该开始使用Explicit Waits