Waits in Selenium

 
Waits in Selenium


Hi All! in this article will walk you through to Waits in Selenium, different types of waits in Selenium Webdriver, and how to implement them.


Waits in Selenium are used to handle sync issues on a webpage. Whenever you open a webpage, the elements of the webpage doesn't load immediately, they take some time to load due to Ajax call. And while running Selenium scripts you have to keep that in mind and pause your code until the element which you are looking for is loaded.

So the amount of time in which you are pausing your code or waiting for the element to appear is Waits in Selenium. Waits are the most commonly asked Selenium Interview Questions if you are preparing for one.



Also, try: Selenium Quiz with Answers


Types of Waits in Selenium

Waits in Selenium is mainly of 2 types.
  1. Implicit Wait
  2. Explicit Wait

1) Implicit Wait :

Implicit wait is applied to all the elements within a webpage(in other words to all the elements of current Webdriver instance). It tells the Webdriver to poll the DOM for a certain amount of time if the element is not available immediately. Below code can be used to define implicit wait :

 driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);  

The above code while finding an element waits for 5 seconds before throwing NoSuchElementException in case the element is not found.

The default value of implicit wait is 0, meaning disabled.

One thing to remember is 5 seconds is the maximum wait time. The webdriver will not wait for the entire 5 seconds, if the element is available at the 3rd second it won't check for it further. It will give results in 3 seconds saving your 2 seconds.

2) Explicit Wait :

Explicit wait is applied to a specific web element until some expected condition is met. This means it keeps on waiting until the expected condition becomes true.

Explicit wait in selenium is of two types WebDriver Wait and Fluent Wait.

a) Webdriver Wait :

 WebDriver driver = new ChromeDriver();  
 WebDriverWait wait = new WebDriverWait(driver,10); //10 Seconds  
 wait.until(ExpectedConditions.elementToBeClickable(By locator));  

Commonly used Expected Conditions

  • elementToBeClickable(WebElement)
  • visibilityOf(WebElement)
  • visibilityOfElementLocated(By locator) 


b) Fluent Wait :

Fluent wait is a type of explicit wait and unlike Webdriver wait, we can have polling time which continuously monitors for the availability of the web element. The ExpectedConditions function is also to be provided by the user.

 WebDriver driver = new ChromeDriver();  
 Wait wait = new FluentWait<WebDriver>(driver)  
              .withTimeout(Duration.ofSeconds(10))  
              .pollingEvery(Duration.ofSeconds(2)) //checks availability every 2 secs 
              .ignoring(NoSuchElementException.class);  
 
WebElement element = wait.until(  
     new Function<WebDriver, WebElement>(){  
        public WebElement apply(WebDriver driver){  
            if(driver.find(By.id("new")).isDisplayed()){  
                return driver.find(By.id("new"));  
            }  
         }  
     }  
 )  



------------------------------------------------------------------------------------------

I will be posting more advanced questions in the upcoming posts. Stay Tuned. Thanks.
Do send your comment if any doubts!