What is the difference between implicit and explicit wait?
I am currently trying to explain selenium WebDriver to one of my colleagues who is new to test automation. During the conversation, he asked me about the difference between implicit and explicit waits in the selenium context. How can I explain it to him?
In the context of selenium, here are the differences given between implicit and explicit wait:-
Implicit wait
It is a tool that is a global setting applied to the WebDriver Instance. It is used in telling the WebDriver to wait for a certain amount of time before throwing a “NoSuchElementException” if there are no elements immediately presented in the DOM.
This can be applied to all elements on the webpage. It can be set using the “implicit _ wait() method on the WebDriver Instance.
Explicit wait
It is more specific and flexible as compared to implicit wait. It can allow you to wait for certain conditions to be met before the process of proceedings with the implementation of the next steps in your test script. You can also specify the condition to wait for and the maximum amount of time to wait.
It can only be applied to the specific element or conditions where they are defined which can provide control over synchronisation.
Here is a simple example given of how you use implicit and explicit waits in Python programming language:-
From selenium import webdriver
From selenium.webdriver.common.by import By
From selenium.webdriver.support.ui import WebDriverWait
From selenium.webdriver.support import expected_conditions as EC
# Implicit Wait
Driver = webdriver.Chrome()
Driver.implicitly_wait(10) # Wait up to 10 seconds if the element is not found immediately
Driver.get(https://example.com)
Element = driver.find_element(By.XPATH, “//div[@id=’some_element’]”)
Print(“Element found using implicit wait:”, element.text)
Driver.quit()
# Explicit Wait
Driver = webdriver.Chrome()
Driver.get(https://example.com)
Wait = WebDriverWait(driver, 10) # Wait up to 10 seconds for the element to be visible
Element = wait.until(EC.visibility_of_element_located((By.XPATH, “//div[@id=’some_element’]”)))
Print(“Element found using explicit wait:”, element.text)
Driver.quit()