何时在Selenium Webdriver中使用显式等待vs隐式等待? [英] When to use explicit wait vs implicit wait in Selenium Webdriver?

查看:143
本文介绍了何时在Selenium Webdriver中使用显式等待vs隐式等待?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用:

driver.manage().timeouts().implicitlyWait(180, TimeUnit.SECONDS);

但是对于以下元素它仍然会连续失败

But it still fails continuously for the below element

    driver.findElement(By.id("name")).clear();
    driver.findElement(By.id("name")).sendKeys("Create_title_01");

我已添加等待代码:

for (int second = 0;; second++) {
        if (second >= 120) fail("timeout");
        try { if (isElementPresent(By.id("name"))) break; } catch (Exception e) {}
        Thread.sleep(1000);
    }

不应该隐式等待等待直到找到元素?
如果我使用显式等待而不是我添加的代码 Thread.sleep()

Shouldn't implicit wait take care of waiting till an element is found? Also would it be better if I use Explicit wait instead of the code I have added that has Thread.sleep()?

推荐答案

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的本地部分运行(使用您的代码的语言)。

  • 适用于您能想到的任何条件。

  • 返回成功或超时错误。

  • 可以将元素的缺失定义为成功条件。

  • 可以自定义重试之间的延迟和要忽略的异常。

  • documented and defined behaviour.
  • runs in the local part of selenium (in the language of your code).
  • works on any condition you can think of.
  • returns either success or timeout error.
  • can define absence of element as success condition.
  • can customize delay between retries and exceptions to ignore.

隐含等待:


  • 未记录且几乎未定义的行为。

  • 在selenium的远程部分(控制浏览器的部分)中运行。

  • 仅适用于find元素方法。

  • 返回找不到的元素或找不到(超时后)。

  • 如果检查元素缺失必须一直等到超时。

  • 除了全局超时外无法自定义。

  • undocumented and practically undefined behaviour.
  • runs in the remote part of selenium (the part controlling the browser).
  • only works on find element(s) methods.
  • returns either element found or (after timeout) not found.
  • if checking for absence of element must always wait until timeout.
  • cannot be customized other than global timeout.

让我们看看在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?

进一步阅读:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆