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

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

问题描述

我正在使用:

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 的本地部分运行(以您的代码语言).
  • 适用于您能想到的任何条件.
  • 返回成功或超时错误.
  • 可以将缺少元素定义为成功条件.
  • 可以自定义重试和要忽略的异常之间的延迟.

隐式等待:

  • 未记录且几乎未定义的行为.
  • 在 selenium 的远程部分(控制浏览器的部分)中运行.
  • 仅适用于查找元素方法.
  • 返回找到的元素或(超时后)未找到的元素.
  • 如果检查元素的缺失必须一直等到超时.
  • 除全局超时外无法自定义.

带有解释的代码示例.第一个隐式等待:

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.

可能的条件示例:elementToBeClickablenumberOfElementsToBeMoreThaninvisibilityOf.以下是内置预期条件的列表: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?

进一步阅读:

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