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

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

问题描述

显式等待示例

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.

几个参考:

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

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.

几个参考:

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

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

隐式等待将为整个轮询DOM树>10 秒,无论 myDynamicElement(或与您的定位器匹配的多个元素)是否在第 4/6/8 秒可见.因此,在这种情况下,您的脚本会延迟 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")));

Explicit Wait 将等待 最多 10 秒,让元素 someid 变为可点击(显示和启用).WebElement 一旦满足 ExpectedConditions 就会返回.如果在配置的时间线的整个持续时间内未满足 ExpectedConditions,您会看到正确的异常.

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天全站免登陆