隐式等待和显式等待之间的内部工作区别是什么 [英] What is the internal working difference between Implicit Wait and Explicit Wait

查看:231
本文介绍了隐式等待和显式等待之间的内部工作区别是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

明确的等待示例

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement myDynamicElement= wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));

隐式等待示例

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"));

假设myDynamicElement在第6秒可见, 因此,在这两种情况下,驱动程序都将等到第6秒,并且控制权将移至连续的书面语句,我想了解在这种情况下,隐式和显式等待彼此之间有何不同?他们如何在内部工作?

Let say myDynamicElement is visible at 6th second, So in both the cases driver will wait till 6th seconds and control will move to the consecutive written statement, I want to understand that how implicit and explicit wait are different from each other in this case? how do they work internally?

推荐答案

隐式等待:

隐式等待是配置 WebDriver 实例来轮询 HTML DOM 尝试配置元素或组/时的配置时间元素的集合(如果不是立即可用).根据当前的 W3C规范,默认时间配置为 0 .我们可以在脚本中的任意位置配置隐式等待的时间/program,并可以根据我们的需要对其进行重新配置.一旦我们设置了隐式等待,在 WebDriver 实例的有效期内有效.

Implicit Wait :

Implicit Wait is the way to configure the WebDriver instance to poll the HTML DOM for a configured amount of time when it tries to find an element or find a group/collection of elements if they are not immediately available. As per the current W3C specification the default time is configured to 0. We can configure the time for the Implicit Wait any where within our script/program and can reconfigure it as per our necessity. Once we set Implicit Wait it will be valid for the lifetime of the WebDriver instance.

一些参考文献:

  • Detailed discussion
  • Detailed documentation.

显式等待是您为 WebDriver 实例定义,配置和实现的代码块,以等待满足特定条件后再继续下一行代码. WebDriverWait以及明确等待.

Explicit Wait is a code block you define, configure and implement for the WebDriver instance to wait for a certain condition to be met before proceeding for the next line of code. WebDriverWait along with certain methods/clauses of ExpectedConditions is one way to implement Explicit Wait.

一些参考文献:

  • Detailed discussion
  • Detailed documentation.

根据您的查询 ...假设myDynamicElement在第6秒可见,因此在这两种情况下,驱动程序都将等到第6秒,并且控制权将移至连续的书面声明...

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

隐式等待将对整个轮询 DOM树 > 10秒,无论在第4、6、8秒时是否显示 myDynamicElement (或与您的定位器匹配的多个元素).因此,在这种情况下,您的脚本会被延迟 4 .

Implicit Wait would poll the DOM Tree for the entire 10 secs irrespective of whether myDynamicElement (or multiple elements matching your locator) is visible at 4th / 6th / 8th second. So, in this case, your script gets delayed by 4 secs.

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement myDynamicElement= wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));

显式等待将等待最长10秒,以使元素someid变为可点击(显示和启用).一旦满足 ExpectedConditions ,就会返回 WebElement .如果在配置的时间轴的整个持续时间内均未满足 ExpectedConditions ,则您会看到正确的 Exception .

Explicit Wait would wait for maximum of 10 secs for the element someid to turn clickable (Displayed and Enabled). The WebElement is returned back as soon as the ExpectedConditions is met. If the ExpectedConditions is not met for the entire duration of the configured timeline, you see the proper Exception.

这篇关于隐式等待和显式等待之间的内部工作区别是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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