gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:35:05 25 4
gpt4 key购买 nike

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

This page意味着它等待整整 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);

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

最佳答案

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

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

我提出了 execute 的要点方法(你可以找到完整的来源 here ):

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
}

该行:
response = executor.execute(command);

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

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

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

关于selenium - Selenium 隐式等待总是占用整个等待时间还是可以更快完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27831757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com