Implicitly Wait v/s Explicit Wait in Selenium WebDriver || Common - Interview Question

Опубликовано: 04 Октябрь 2024
на канале: Arun Kapoor
1,462
36

Implicit Wait v/s Explicit Wait in Selenium WebDriver || Common - Interview Question

Q. Why Do We Need Waits In Selenium?

Most of the web applications are developed using Ajax and Javascript. When a page is loaded by
the browser the elements which we want to interact with may load at different time intervals.
Not only it makes this difficult to identify the element but also if the element is not located it will throw an "ElementNotVisibleException" exception. Using Selenium Waits, we can resolve this problem.
Let's consider a scenario where we have to use both implicit and explicit waits in our test.
Assume that implicit wait time is set to 20 seconds and the explicit wait time is set to 10 seconds.
Suppose we are trying to find an element which has some "ExpectedConditions "(Explicit Wait),
If the element is not located within the time frame defined by the Explicit wait(10 Seconds),
It will use the time frame defined by implicit wait(20 seconds) before throwing an
"ElementNotVisibleException".
#Selenium Web Driver Waits
1. Implicit Wait
2. Explicit Wait
1. Implicit Wait in Selenium
The Implicit Wait in Selenium is used to tell the web driver to wait for a certain amount of time before it throws a "No Such Element Exception". The default setting is 0. Once we set the time, the web driver will wait for the element for that time before throwing an exception.
Selenium Web Driver has borrowed the idea of implicit waits from Watir.
In the below example we have declared an implicit wait with the time frame of 10 seconds. It means that if the element is not located on the web page within that time frame, it will throw an exception.

To declare implicit wait:

Syntax:

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

2. Explicit Wait in Selenium

The Explicit Wait in Selenium is used to tell the Web Driver to wait for certain conditions
(Expected Conditions) or maximum time exceeded before throwing "ElementNotVisibleException"
exception.
It is an intelligent kind of wait, but it can be applied only for specified elements.
It gives better options than implicit wait as it waits for dynamically loaded Ajax elements.
Once we declare explicit wait we have to use "ExpectedConditions" or we can configure how
frequently we want to check the condition using Fluent Wait.
These days while implementing we are using Thread.Sleep() generally it is not recommended to use
In the below example, we are creating reference wait for "WebDriverWait" class and instantiating
using "WebDriver" reference, and we are giving a maximum time frame of 20 seconds.

Syntax:

WebDriverWait wait = new WebDriverWait(WebDriverRefrence,TimeOut);

Interview Question: Which wait we have to use in our Automation Framework?
or Which wait is better Implicit wait or Explicit wait?

#ITIndustryTutorials || ImplicitWait and ExplicitWait in Selenium || Difference between Implicit Wait and Explicit Wait