This question is asked in almost all selenium interview questions because many web applications use AJAX.
Selenium Webdriver introduces the concept of waits for AJAX-based applications. There are two waiting methods, implicit wait, and explicit wait
When an implicit wait is implemented in tests, if WebDriver cannot find an element in the Document Object Model (DOM), it will wait for a defined amount of time for the element to appear in the DOM. In other terms, an implicit wait polls the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.
Implicit waits can slow down your tests because once set, the implicit wait is set for the life of the WebDriver object’s instance. This means that when an application responds normally, it will still wait for each element to appear in the DOM which increases the overall execution time.
Another downside is if for example you set the waiting limit to be 5 seconds and the elements appears in the DOM in 6 seconds, your tests will fail because you told it to wait a maximum of 5 seconds.
An example of an implicit wait is:
Explicit waits are better than implicit wait. Unlike an implicit wait, you can write custom code or conditions for a wait before proceeding further in the code.
An explicit wait can be used where synchronization is needed, for example, the page is loaded but we are still waiting for a call to complete and an element to appear.
Selenium WebDriver provides WebDriverWait and ExpectedCondition classes for implementing an explicit wait. The ExpectedCondition class provides a set of predefined conditions to wait before proceeding further in the code.
An example of an explicit wait is:
WebDriverWait wait = new WebDriverWait(driver, 10);
Implicit wait time is applied to all elements in your script and Explicit wait time is applied only for a particular specified element.