WAITS - Yash-777/SeleniumDriverAutomation GitHub Wiki

WebDriver can generally be said to have a blocking API. Because it is an out-of-process library that instructs the browser what to do, and because the web platform has an intrinsically asynchronous nature, WebDriver doesn't track the active, real-time state of the DOM.

Selenium and WebDriver are connected to race conditions that occur between the browser and the user's instructions.


Explicit waiting

Explicit waits are available to Selenium clients for imperative, procedural languages. They allow your code to halt program execution, or freezing the thread, until the condition you pass it resolves. The condition is called with a certain frequency until the timeout of the wait is elapsed. This means that for as long as the condition returns a falsy value, it will keep trying and waiting.

So, the explicit wait is set to the Specific element/action.


Implicit waiting

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0, meaning disabled. Once set, the implicit wait is set for the life of the session.

Warning: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example setting an implicit wait of 10 seconds and an explicit wait of 15 seconds could cause a timeout to occur after 20 seconds.


FluentWait

FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition, as well as the frequency with which to check the condition.

User may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.

driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.MINUTES);

// Explicit waiting - Per Element
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(locator)));

// Implicit waiting - Per Session
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);


// Fluent Wait - Per Element
// Waiting 30 seconds for an element to be present on the page, checking for its presence once every 5 seconds.
final String locator;
FluentWait<WebDriver> wait = new org.openqa.selenium.support.ui.FluentWait<WebDriver>(driver)
  .withTimeout(30, TimeUnit.SECONDS)
  .pollingEvery(5, TimeUnit.SECONDS)
  .ignoring(NoSuchElementException.class);

WebElement element = wait.until(new com.google.common.base.Function<WebDriver, WebElement>() {
  public WebElement apply(WebDriver driver) {
	return driver.findElement(By.xpath( locator ));
  }
});

element.sendKeys("text to enter");
⚠️ **GitHub.com Fallback** ⚠️