Implicit Wait

In Previous section, we have discussed about taking screenshot for full page as well as visible area of webpage in Selenium Python. Now we will discuss about handing Implicit Wait in Selenium.

Objective:

  • What is Wait
  • Types of Wait
  • Implicit Wait
  • Implementation of Implicit wait
  • Disadvantage of Implicit wait

What is Wait?

Now a day’s most of the application is developed in advanced technology like Ajax, JavaScript and many more. This can result the page loading slow.  Also every script will take different time to load the page. As a result your page loading time will be different for different page.

In this scenario, if you want to handle the locator in page in selenium, you may get “ElementNotVisibleException” exception because of page loading time. If page is not loaded properly, you may not get the proper element which you want to search through xpath or any other locator strategy.

To overcome this troubleshoot we will implement waits in Selenium so that script will wait for specified time and run after waits completes.

How Wait is differ from sleep()?

If we will use time.sleep(), then it is a static wait. It will wait for certain amount of time. But now a day’s application is very advanced. There are lots of Ajax components , loading components like lazy loading, calculation on backend. Because of this elements may take some time to visible on page. The time it will take is not same. In this case time.sleep() will not work.

Let’s think for a scenario in which sometime a web element will take 5 sec to load and in other the same element will take 9 sec to load onto the page. If we will define time.sleep(5) then it will throw No Such Element Exception” Exception for which it takes 9 sec to load. In other words if we will use time.sleep(10) then it will wait more than required for those while it will load in 5 sec.

To handle we can think that, we can declare of maximum time let’s say here 10 sec and if element is found within the 10sec then you can process to the next step. This concept is called dynamic wait. If element will be found in 4 sec then another 6 sec will be saved for your test case running.

Types of Waits:

There are 2 types of waits present in Selenium.

  • Implicit
  • Explicit
Implicit Wait

In this tutorial we will discuss about the implicit wait.

Implicit Waits:

Out of 2 waits which are listed above, implicit wait is the default wait in Selenium. The default value of implicit wait is 0. If you want to give some custom time then you can give as parameter as below. This is considered as 10. It will be applied to all the element available in the page. That means when the driver will go to interact with any element for that particular application, then throughout the application the driver will considered as implicit wait defined. That’s why this is called Global wait.

One thing you need to remember that this will be applicable only for the web elements. It will not applicable for other than web element such as title, URL, alert etc.

driver.implicit_wait(10)

To understand implicit wait we will use the website https://app.hubspot.com/login. When you launch this URL, it will take some time to load and display login page.

Let’s run our script without implicit wait.

from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chromeimport ChromeDriverManager


driver = webdriver.Chrome(ChromeDriverManager().install())
#driver.implicitly_wait(10)
driver.get('https://app.hubspot.com/login')
driver.maximize_window()
uname = driver.find_element(By.ID, 'username')
uname.send_keys('Pratik')

Output:

Implicit Wait

From the above example you can see it is throwing an error like No such Element. This is because when you open the above URL, it is taking some time to load login page. Now let’sadd out implicit wait of 10 sec and see what will happen.

from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chromeimport ChromeDriverManager


driver = webdriver.Chrome(ChromeDriverManager().install())
driver.implicitly_wait(10)
driver.get('https://app.hubspot.com/login')
driver.maximize_window()
uname = driver.find_element(By.ID, 'username')
uname.send_keys('Pratik')

Output:

Implicit Wait
Implicit Wait

In the above example you can notice that though we have given 10 sec in Implicit wait, our page got loaded in 5 sec and immediately after 5 sec next line got executed.

There is a default internal time interval in Selenium is 500 ms to go to browser and check for elements.

Let’s have a look whether get.title is taking implicit wait or not.

from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chromeimport ChromeDriverManager


driver = webdriver.Chrome(ChromeDriverManager().install())
driver.implicitly_wait(10)
driver.get('https://app.hubspot.com/login')
print(driver.title)
driver.maximize_window()
uname = driver.find_element(By.ID, 'username')
upass = driver.find_element(By.ID, 'password')

uname.send_keys('Pratik')

Output:

user name and Password

Here you can see title showing immediately rather wait for implicit wait.

Disadvantage of Implicit Waits:

  • Implicit wait generally slow down the performance of the Script as the drives will wait for certain amount of time.
  • Default time is 0. If we set some time other than 0 then it will stick to that time only. To change the time, we need to declare the implicit wait again.

Summary:

  • time.sleep() is a static wait.
  • This wait is called as Global wait.

Leave a Reply

Your email address will not be published. Required fields are marked *