更新时间:2022-02-25 01:37:25
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:
显式等待:
隐含等待:
让我们看看在selenium的实际源代码中显式等待和隐式等待之间的区别。我复制了selenium的python绑定代码,因为python易于阅读。
Let's see the difference between explicit wait and implicit wait in the actual source code of selenium. I copied the code from the python binding of selenium because python is "easy to read".
代码 WebDriverWait.until()
(显式等等):
The code of WebDriverWait.until()
(explicit wait):
def until(self, method, message=''):
end_time = time.time() + self._timeout
while(True):
try:
value = method(self._driver)
if value:
return value
except self._ignored_exceptions:
pass
time.sleep(self._poll)
if(time.time() > end_time):
break
raise TimeoutException(message)
现在使用人类语言:显式等待需要一个返回a的方法如果成功,那就很有价值。然后它重复执行给定的方法,其间有延迟。抑制了给定方法的预期误差。如果给定方法返回truish值,则显式wait将返回该值。如果时间用完,则会引发超时异常。
Now in human language: explicit wait expects a method which returns a truish value if successful. It then repeatedly executes the given method with a delay in between. Expected errors from the given method are supressed. If the given method returns a truish value then explicit wait will return that value. If the time runs out a timeout exception is raised.
与 WebDriver.implicitly_wait()
(为简洁起见,删除了评论):
Compare to the code of WebDriver.implicitly_wait()
(comments removed for brevity):
def implicitly_wait(self, time_to_wait):
self.execute(Command.IMPLICIT_WAIT, {'ms': float(time_to_wait) * 1000})
self.execute()
是 WebDriver.execute()
,它调用 RemoteConnection.execute()
反过来,据我所知,一个到硒的远端的RPC。
self.execute()
is WebDriver.execute()
which calls RemoteConnection.execute()
which in turn does, as far as I can tell, an RPC to the remote side of selenium.
在人类语言中:隐式等待将消息发送到selenium webdriver的远程端。 selenium webdriver的远程端是selenium的一部分,它实际上控制着浏览器。远程端对消息做了什么? 这取决于。这取决于操作系统和浏览器以及selenium的版本。据我所知,不能保证特定实现的实际行为。
In human language: the implicit wait sends a message to the "remote side" of the selenium webdriver. The remote side of the selenium webdriver is the part of selenium which is actually controlling the browser. What does the remote side do with the message? "It depends". It depends on the operating system and on the browser and on the version of selenium. As far as I can tell there is no guarantee about the actual behaviour of a specific implementation.
可能的实现是:
请注意,隐式等待仅对查找元素方法生效。
Note that implicit wait only takes effect on find element(s) methods.
我没有查看selenium远程端的实际源代码。通过阅读中的评论来收集信息。关于硒中隐式和显式等待的错误报告:
I have not looked up the actual source code of the remote sides of selenium. The information is gathered from reading the comments in the bug reports about implicit and explicit wait in selenium:
我的结论:隐性等待很糟糕。能力有限。该行为是未记录的,并且依赖于实现。
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 overhead because of multiple remote procedure calls. Also the explicit wait is a bit more verbose. But that verbosity makes the code explicit. And explicit is better that implicit. Right?
进一步阅读: