更新时间:2022-12-15 15:49:39
TL;DR:始终使用显式等待.忘记隐式等待的存在.
TL;DR: Always use explicit wait. Forget that implicit wait exists.
以下是显式等待和隐式等待之间差异的简要概述:
Here is a quick rundown on the differences between explicit and implicit wait:
显式等待:
隐式等待:
带有解释的代码示例.第一个隐式等待:
Code examples with explanation. First implicit wait:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
现在显式等待:
WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement myDynamicElement = wait.until(
ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
两个代码示例都做同样的事情.找到某个元素并在 10 秒后未找到则放弃.隐式等待只能做到这一点.它只能尝试找到超时的元素.显式等待的优势在于它可以等待各种条件.还可以自定义超时并忽略某些异常.
Both code examples do the same thing. Find a certain element and give up if not found after 10 seconds. The implicit wait can do only this. It can only try to find an element with a timeout. The strength of explicit wait is that it can wait for all kinds of conditions. Also customize timeout and ignore certain exceptions.
可能的条件示例:elementToBeClickable
、numberOfElementsToBeMoreThan
或 invisibilityOf
.以下是内置预期条件的列表:https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html
Example of possible conditions: elementToBeClickable
, numberOfElementsToBeMoreThan
or invisibilityOf
. Here is a list of the built in expected conditions: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html
更多解释:
隐式等待超时仅对 findElement*
方法有效.如果设置,则所有 findElement*
将在声明无法找到元素之前等待"设置的时间.
The implicit wait timeout has effect only on findElement*
methods. If set then all findElement*
will "wait" for the set time before declaring that the element cannot be found.
findElement*
将如何等待尚未定义.这取决于浏览器或操作系统或 selenium 版本.可能的实现是:
How a findElement*
will wait is not defined. It depends on browser or operating system or version of selenium. Possible implementations are:
这个列表是通过观察和阅读错误报告以及对 selenium 源代码的粗略阅读收集的.
This list is gathered from observations and reading bug reports and cursory reading of selenium source code.
我的结论:隐式等待是不好的.能力有限.该行为未记录在案且依赖于实现.
My conclusion: Implicit wait is bad. The capabilities are limited. The behaviour is undocumented and implementation dependent.
显式等待可以完成隐式等待可以做的一切,甚至更多.显式等待的唯一缺点是代码更冗长.但是这种冗长使代码变得明确.显式优于隐式.对吗?
Explicit wait can do everything implicit wait can and more. The only disadvantage of explicit wait is a bit more verbose code. But that verbosity makes the code explicit. And explicit is better that implicit. Right?
进一步阅读: