During script execution, if elements in the webpage or webpage itself are not loaded or visible completely but selenium tries to perform some actions upon them, then it will throw “NoSuchElementException”, because selenium works on the element which are successfully loaded and visible.
To overcome this issues selenium provided three different
type of wait command in selenium
1) Implicit Wait
2) Explicit Wait
3) Fluent Wait
Implicit Wait: -In
selenium WebDriver, the idea of implicit wait has been influenced from waitr.Implicit
wait can accept the time amount in seconds,
milliseconds, nanoseconds, minute etc.
It depends on your requirement.
Implicit wait sets as default for selenium WebDriver. An
exception will be thrown if it is not able to find out any element within the specific
amount of time. This is applicable for each element in the application.
When to Use Implicit
Wait: During implicit wait time, it
tries to locate the element in the Webpage. If WebDriver does not find the
element initially. Then it doesn’t try to identify the element again, instead
it waits till the specified time is over. In the end moment when time is over,
it checks once again if element is present before throwing the exception. The
default setting is zero. It is default time which is applied throughout the
execution session and only gets destroyed when the WebDriver instance is
destroyed.
Explicit Wait: - This wait gives you the options to wait
until a desired condition is satisfied. You can use some of prebuilt ExpectedConditions for
the elements to be visible, invisible, clickable etc.
When to Use Explicit Wait: - It
should be applied for those specific elements which takes more time to be
displayed comparing to other elements in the application. Suppose, we have
observed that one element is taking around 1 minute time to be displayed. Then
we cannot apply implicit wait for 1 minute because then it will wait for same
amount of time for every element. Instead, we will use explicit wait with the
expected condition.
Fluent Wait:- FluentWait contains 3 parameters.
WithTimeOut: defines
the maximum amount of time it will wait for any element to be visible.
pollingEvery’
: takes time as an argument e.g. 5 seconds. It will check the status of
element after every 5 seconds.
ignoring:
we can define the type of exceptions need to be ignored during element search.
When to Use Fluent Wait:- Suppose, there are few elements which
sometimes takes more than 1 minute to display or sometimes gets displayed
within 2 seconds. For such elements, it would be better to use fluent wait. As
it will keep checking the presence of those elements after a specified amount
of time until it finds it or time out.
FluentWait can prove pretty handy for Ajax
because of the flexibility you have for ignoring exceptions and easily tweaking
the wait and polling times.