What is Explicit wait? How to Achieve Explicit wait in Selenium?

What is Explicit wait in Selenium WebDriver?

 

Explicit wait is a type of synchronization in selenium, which is webelement specific and not applied to driver for his life. Different elements on the page takes different times to load on the page, if we use implicit wait for 10 Sec there is no guarantee that all the elements loaded within 10 sec, some elements may take more than 1 minute, in such conditions Explicit waits can be used to wait for that particular element.
Explicit Wait in-Selenium-Webdriver
We can tell the tool to wait only till the Condition satisfy. Once the condition is satisfying, the tool proceed with the next step. This can be done with WebDriverWait in conjunction with ExpectedConditions Class.
Logic: –
It is applicable to the given control and waits to the maximum amount of time or move forward in case the given condition is met.

We are explicitly making the execution of selenium code to wait, for a certain condition to occur before, proceeding further in the code.

Syntax:

WebDriverWait.until(condition-that-finds-the-element(ExpectedCondition))

WebDriverWait by default calls the Expected Condition every 500 milliseconds until it returns successfully. Expected condition can be in the form of like:
button is disabled/enabled
textbox is visible
alertispresentExample for Explicit Wait

package demoPackage1;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

public class ExplicitWait {

  @Test
  public void ExplicitWaitTest(){
    
    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    
    driver.navigate().to("http://demowebshop.tricentis.com/register");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
    //define webdriver wait statement with time specified
    WebDriverWait wait = new WebDriverWait(driver, 10);
    
    //apply the wait for specific element. Below statement will wait for element to be present till 10 sec as specified by above
    wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy((By.xpath("//a[@class='ico-register']"))));
    
    driver.close();
    driver.quit();
  }
}

 

Explicit Wait in Selenium WebDriver

Hope This Helps !!!!

Leave Comment

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

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories