Base on Python implementation
Scenarios
When a page is loaded by the browser, the elements within that page may load at different time intervals. This makes locating elements difficult: if an element is not yet present in the DOM, a locate function will raise an ElementNotVisibleExceptionexception. Using waits, we can solve this issue. Waiting provides some slack between actions performed - mostly locating an element or any other operation with the element.
Long story short
#1, wait until DOM is ready
#2, wait for complex page with JavaScript to load
Methods
Selenium Webdriver provides two types of waits - implicit & explicit. An explicit wait makes WebDriver wait for a certain condition to occur before proceeding further with execution. An implicit wait makes WebDriver poll the DOM for a certain amount of time when trying to locate an element.
Implicit Wait
Implicit Wait:implicit wait provide to load DOM object for a particular of time before trying to locate element on page. Default implicit wait is 0. We need to set implicit wait once and it apply for whole life of Webdriver object. Add below line of code in test for implicit wait. However implicit wait slow down execution of your test scripts if your application responding normally.
Example
Explicit Wait + Expected Conditions
Explicit Wait:In explicit wait you can write custom code for a particular element to wait for particular time of period before executing next steps in your test. This provide you better option than implicit wait. Webdriver provide “WebDriverWait”, “ExpectedCondition” classes to implement this.ExpectedConditions class provide some set of predefine condition to wait element.
Example
This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.
Expected Conditions
There are some common conditions that are frequent when automating web browsers. Listed below are Implementations of each. Selenium Python binding provides some convienence methods so you don’t have to code an expected_condition class yourself or create your own utility package for them
waits in Python
time.sleep()
Thread.sleep()- It will sleep time for script,not good way to use in script as it's sleep without condition. What if 2 seconds are not enough in 5% of the cases?
Comparison
Here is a quick rundown on the differences between explicit and implicit wait:
Explicit wait:
- documented and defined behaviour.
- runs in the local part of selenium (in the language of your code).
- works on any condition you can think of.
- returns either success or timeout error.
- can define absence of element as success condition.
- can customize delay between retries and exceptions to ignore.
Implicit wait:
- undocumented and practically undefined behaviour.
- runs in the remote part of selenium (the part controlling the browser).
- only works on find element(s) methods.
- returns either element found or (after timeout) not found.
- if checking for absence of element must always wait until timeout.
- cannot be customized other than global timeout.
Problems
Combining implicit wait and explicit wait together results in unexpected wait times
Don't mix implicit and explicit waits. Part of the problem is that implicit waits are often (but may not always be!) implemented on the "remote" side of the WebDriver system. That means they're "baked in" to IEDriverServer.exe, chromedriver.exe, the WebDriver Firefox extension that gets installed into the anonymous Firefox profile, and the Java remote WebDriver server (selenium-server-standalone.jar). Explicit waits are implemented exclusively in the "local" language bindings. Things get much more complicated when using RemoteWebDriver, because you could be using both the local and remote sides of the system multiple times
This is how that would work: local code -> Java remote server -> local Java language bindings on the remote server -> "remote" component like the Firefox extension, chromedriver.exe or IEDriverServer.exe. It's even more complex in the grid case, since there could be other hops in between.
Thus, when you try to mix implicit and explicit waits, you've strayed into "undefined behavior". You might be able to figure out what the rules of that behavior are, but they'll be subject to change as the implementation details of the drivers change. So don't do it.
You shouldn't be experiencing "hangs" when an element can't be found if you're not using implicit waits. The driver should throw a NoSuchElement exception immediately.