Waits in Selenium
Waits in selenium are used whenever there is a loading wait is required in the automation script. Waits can be hard type or soft type. Most of the time only soft type wait is used in automation scripts because waiting for the defined time in hard wait is not a good practice. The action should be performed on an element as soon as it is intractable. Waits prevent the failure of automation scripts.
Why do we need waits in selenium?
While working on automation scripts of any project, there are some errors that occur, and tests get failed. The reason behind these errors is that when the script run, the web page takes some time to load. And the element with which we want to interact with is currently not present on the page. But the script doesn’t wait for the element to be intractable. In this situation, the test gets failed. Selenium waits are the shortcodes that provide a wait to load the page completely or wait until the element is intractable.
Below is an example code without wait which throws an error ElementNotInteractableException
In the below code we are trying to open ebay.com and using action class we hover the mouse to the electronics category. After hovering the mouse on electronics, it will show subcategories. We are trying to open the subcategory Apple by clicking on that using an automation script without using any kind of wait.
After running the script, we got an error (element not intractable) as shown below. The error was generated because the element is not visible after immediate hover of the mouse in the electronics category.
Types of Waits in Selenium
Here we will see the types of waits in selenium:
The implicit wait is applicable to all further statements or further script web elements that are available in your script, you just have to implement implicit wait once in your script, and then it will be applicable for all the web elements that are there on the page and it will look for or wait for the availability of those web element or visibility of those web elements for the amount of time that you specify.
Now let’s apply the implicit wait on the above code and check the result.
In line 16, we added an implicit wait of 10 seconds and ran the code. The result is as below:
By implementing the wait of 10 seconds, the web driver waited until the element is intractable and clicked as soon as it is interactable.
There are scenarios in the actual web interfaces, where there are some web Elements, which take quite a while to load but there are other web Elements that load pretty faster.
In that case, you can’t say, that there are one or two web elements on a page that takes 15 to 20 seconds. So would you just go ahead and change the implicit wait to 20 seconds and what if those web elements take even more than 20 seconds or 30 seconds? So you can’t just keep going and changing implicit weight according to the max time that is taken by one of the web elements. So that’s where explicit wait, comes into the picture and explicit, wait specifies, the wait for that particular web element or web elements on the page, you can specify for which particular web element you want to wait for that long. And that’s where we will implement the explicit wait.
Now let’s apply it to the above code:
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("Paste your xpath here"))).click();
In the previous explanation, when we set up the explicit wait, we had specified the time that we want to wait or the timeout that we have specified in that case like 10 seconds for waiting for a particular web element, right? But in that case, there was no option to pole, you don’t know how many times the polling will happen when the web drivers try to find this web element, right? You don’t know whether it’s polling about one second, half a second, or five seconds. So, the fluent wait has the capability to basically provide the link time as well. So you can provide the time out, and then you can also specify pole time. So what Webdriver will do is, it will wait for that particular amount of time.
For example, if you specify two seconds of polling time, it will wait for two seconds and then poll for that web element. After 2 seconds, if it is able to find it, it will proceed, if it is not then again wait for two seconds and then try to find out after 2 seconds. So that’s how the fluent wait works.
Now let’s apply it to the above code:
In the above code, we have implemented a timeout of 10 seconds and polling of every two seconds. Polling means the driver will try to search the element every 2 seconds.
As shown above, we can use three types of waits in Selenium in our code as per the requirements. The most used waits are implicit and explicit waits. We should not use thread. sleep in our script because it is not good practice. If an element is taking time more than the other, then the explicit wait is best to use in the script.