Java Selenium WebDriver 中的隐式等待和显式等待有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22656615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:08:02  来源:igfitidea点击:

What is difference between Implicit wait and Explicit wait in Selenium WebDriver?

javaseleniumselenium-webdriver

提问by tsivarajan

There are Implicit and Explicit wait in Selenium WebDriver. What's the difference between them?

Selenium WebDriver 中有隐式和显式等待。它们之间有什么区别?

Kindly share the knowledge about Selenium WebDriver. Please show the real time example with Implicit & Explicit wait.

请分享有关 Selenium WebDriver 的知识。请显示带有隐式和显式等待的实时示例。

采纳答案by Manigandan

Check the below links:

检查以下链接:

  • Implicit Wait- It instructs the web driver to wait for some time by poll the DOM. Once you declared implicit wait it will be available for the entire life of web driver instance. By default the value will be 0. If you set a longer default, then the behavior will poll the DOM on a periodic basis depending on the browser/driver implementation.

  • Explicit Wait+ ExpectedConditions- It is the custom one. It will be used if we want the execution to wait for some time until some condition achieved.

  • Implicit Wait- 它通过轮询 DOM 指示 Web 驱动程序等待一段时间。一旦您声明了隐式等待,它将在 Web 驱动程序实例的整个生命周期中可用。默认情况下,该值为 0。如果您设置更长的默认值,则该行为将根据浏览器/驱动程序实现定期轮询 DOM。

  • Explicit Wait+ ExpectedConditions- 这是自定义的。如果我们希望执行等待一段时间直到达到某个条件,它将被使用。

回答by Sathish D

Implicit wait--

隐式等待——

Implicit waits are basically your way of telling WebDriver the latency that you want to see if specified web element is not present that WebDriver looking for. So in this case, you are telling WebDriver that it should wait 10 seconds in cases of specified element not available on the UI (DOM).

隐式等待基本上是您告诉 WebDriver 您想要查看的延迟的方式,如果指定的 Web 元素不存在 WebDriver 正在寻找。因此,在这种情况下,您告诉 WebDriver,如果指定元素在 UI (DOM) 上不可用,它应该等待 10 秒。

Explicit wait--

显式等待——

Explicit waits are intelligent waits that are confined to a particular web element. Using explicit waits you are basically telling WebDriver at the max it is to wait for X units of time before it gives up.

显式等待是仅限于特定 Web 元素的智能等待。使用显式等待,您基本上是在告诉 WebDriver 在它放弃之前最多等待 X 个时间单位。

回答by Aditi

Adding another point of view to above mentioned solutions.

为上述解决方案添加另一个观点。

Implicit Wait:When created, is alive until the WebDriver object dies. And is like common for all operations.

隐式等待:创建时,在 WebDriver 对象死亡之前一直处于活动状态。并且对于所有操作都是通用的。

Whereas,
Explicit wait, can be declared for a particular operation depending upon the webElement behavior. It has the benefit of customizing the polling time and satisfaction of the condition.
For example, we declared implicit Wait of 10 secs but an element takes more than that, say 20 seconds and sometimes may appears on 5 secs, so in this scenario, Explicit wait is declared.

而可以根据 webElement 行为为特定操作声明显
式等待。它具有自定义轮询时间和条件满足的好处。
例如,我们声明了 10 秒的隐式等待,但一个元素需要更多时间,比如 20 秒,有时可能出现在 5 秒,因此在这种情况下,声明了显式等待。

回答by Anchal Agrawal

Differences:

区别:

1) Implicit wait is set for the entire duration of the webDriver object. Suppose , you want to wait for a certain duration, let's say 5 seconds before each element or a lot of elements on the webpage load. Now, you wouldn't want to write the same code again and again. Hence, implicit wait. However, if you want to wait for only one element, use explicit.

1) 在 webDriver 对象的整个持续时间内设置隐式等待。假设,您想等待一定的时间,比如在加载网页上的每个元素或许多元素之前 5 秒。现在,您不想一次又一次地编写相同的代码。因此,隐式等待。但是,如果您只想等待一个元素,请使用显式。

2) You not only need web element to show up but also to be clickable or to satisfy certain other property of web elements. Such kind of flexibility can be provided by explicit wait only. Specially helpful if dynamic data is being loaded on webpage. You can wait for that element to be developed (not just show up on DOM) using explicit wait.

2)您不仅需要显示网页元素,还需要可点击或满足网页元素的某些其他属性。这种灵活性只能通过显式等待来提供。如果在网页上加载动态数据,则特别有用。您可以使用显式等待来等待该元素被开发(不仅仅是出现在 DOM 上)。

回答by Vish Shady

My Thought,

我的想法,

Implicit Wait: If wait is set, it will wait for specified amount of time for each findElement/findElements call. It will throw an exception if action is not complete.

隐式等待:如果设置了等待,它将为每个 findElement/findElements 调用等待指定的时间。如果操作未完成,它将抛出异常。

Explicit Wait: If wait is set, it will wait and move on to next step when the provided condition becomes true else it will throw an exception after waiting for specified time. Explicit wait is applicable only once wherever specified.

显式等待:如果设置了等待,当提供的条件为真时,它将等待并进入下一步,否则它将在等待指定时间后抛出异常。显式等待仅在指定的地方适用一次。

回答by Andy

Implicit waits are used to provide a default waiting time between each consecutive test step/command across the entire test script. Thus, subsequent test step would only execute when the specified amount of time have elapsed after executing the previous test step/command.

隐式等待用于在整个测试脚本的每个连续测试步骤/命令之间提供默认等待时间。因此,后续的测试步骤只会在执行前一个测试步骤/命令后经过指定的时间量时执行。

Explicit waits are used to halt the execution till the time a particular condition is met or the maximum time has elapsed. Unlike Implicit waits, Explicit waits are applied for a particular instance only.

显式等待用于暂停执行,直到满足特定条件或经过最长时间。与隐式等待不同,显式等待仅适用于特定实例。