Implicit wait() is used to inform WebDriver that there could be cases When a page is loaded by the browser, the elements which we want to interact with may not load instantaneously. In those cases WebDriver has to continuously listen to DOM for a given amount of time until either the time expires or the element is found.
In implicit wait, we will actually define a wait time globally, so it is applied to all the elements in the script. Below Implicit Wait() says driver that hey wait for N number of seconds before you throw an error or exception.
Explicit Wait(): Explicit Wait will make your code to wait for certain condition to occur before moving forward.
Explicit Wait() is 2 types
- WebDriverWait :It is used to tell the Web Driver to wait for certain conditions (Expected Conditions) or the maximum timeout.
WebDriverWait wait = new WebDriverWait(driver, TimeOut); wait.until(ExpectedConditions.visiblityOfElementLocated(locator));
The following are the suggestions if you type ExpectedConditions.
2. Fluent Wait : fluent wait is used to tell the web driver to wait for a condition, as well as the frequency or poll with which your WebDriver monitors or listens to DOM.
Frequency or poll : Setting up a repeat cycle with the time frame to verify/check the condition at the regular interval of time
Wait wait = new FluentWait(driver) .withTimeout(30, SECONDS) .pollingEvery(5, SECONDS) .ignoring(NoSuchElementException.class);
Ex: fluentwait 10 sec, polling 2 sec means every 2 sec it checks DOM for the element
Conclusion: Explicit Waits are smarter than Implicit Waits because they don’t wait for the max time out. Instead it waits for the time till the condition becomes true.