Selenium Wait, Implicit and Explicit Waits

Hasan Ozyer
3 min readJul 19, 2023

--

Today i will try to Explain the What is Implicit wait, What is Explicit wait and their difference from each other. Let’s first start with why there was a need for these Waits.

If you are like me, When you try to automate a certain web application, you might have already faced the problem that caused these methods/classes to be created. It is the Synchronization Problem.

💡 What is Synchronization Problem?

Let’s say you have written multiple lines of code for an automation script. You are automating the amazon’s official site for shopping. You entered the landing page, you automated it, you went in to another page etc.

When you try to execute your code, your selenium automation script will be a lot faster than response of your application most of the times. Especially now in Selenium 4, WebDriver APIs adopt the W3C standardization. Since major browser drivers such as geckodriver, chromedriver, etc. , follow the W3C standard, the WebDriver in Selenium 4 will directly communicate with the web browser. So it has gotten a lot faster than it used to be.

So there will not be balance between your automation java script and your application. Most of the times your automation will be a lot faster than your application’s reponse. So that is one of the reasons that your automation script will definitely fail sometimes. Because of the element will not be available by the time. This is a synchronization problem. To solve this problem we have multiple types of waits available in selenium.

  • Thread.sleep(MS)
  • Implicit Wait
  • Explicit Wait
  • Fluent Wait

💡 Thread.sleep(MS)

❗ This sleep belongs to Java. This should not be confused with an elementWait, It stops the Java execution. It is advised not to use this in Automation because it completly stops the java code.

💡 Implicit Wait

❗ Selenium waits until driver.findElement(); finds an element

❗ It Belongs to driver, it is defined in driver. It is defined once, works for every element. It looks like this;

  • driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20));

❗ It waits until the element is present (**presence**) With a given amount of time(In this case, it is 20 Sec)

❗ In this time period within every 500 milisecond it searches for the element.

  • If it finds the element it continues without giving error.
  • If it could not find the element within given tamount of time, it throws error.

💡 Explicit Wait

❗ It is used for spesificially every element situations.

❗ It is defined seperately as ``WebDriverWait wait;``

❗ It is used with WebElement or Locator (By).

❗ It is used for the cases like below;

  • visibility
  • clickability
  • attribute
  • invisibility
  • presence
  • elements size
  • ….

❗ The definement;

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(“id”)));
element.click();

❗ The examples for Explicit wait;

  • ExpectedConditions.presenceOfElementLocated();
  • ExpectedConditions.presenceOfAllElementsLocatedBy();
  • ExpectedConditions.visibilityOf();
  • ExpectedConditions.visibilityOfAllElementsLocatedBy();
  • ExpectedConditions.visibilityOfAllElements();
  • ExpectedConditions.elementToBeClickable();
  • ExpectedConditions.elementToBeClickable();
  • ExpectedConditions.invisibilityOfElementLocated();
  • ExpectedConditions.invisibilityOf();
  • ExpectedConditions.alertIsPresent();
  • ExpectedConditions.attributeContains();
  • ExpectedConditions.attributeContains();
  • ExpectedConditions.attributeToBe();
  • ExpectedConditions.attributeToBe();
  • ExpectedConditions.elementToBeSelected();
  • ExpectedConditions.elementToBeSelected();
  • ExpectedConditions.numberOfElementsToBe();
  • ExpectedConditions.numberOfElementsToBeLessThan();
  • ExpectedConditions.numberOfElementsToBeMoreThan();
  • ExpectedConditions.numberOfWindowsToBe();
  • ExpectedConditions.titleIs();
  • ExpectedConditions.attributeToBeNotEmpty();
  • ExpectedConditions.invisibilityOfElementWithText();
  • ExpectedConditions.textMatches();
  • ExpectedConditions.textToBe();
  • ExpectedConditions.textToBePresentInElement();
  • ExpectedConditions.textToBePresentInElementLocated();
  • ExpectedConditions.titleContains();

❗ An example for ExplicitWait(Below methods does the same job);

public void click(By locator){
wait.until(ExpectedConditions.elementToBeClickable(locator)).click();
}
private void click(By locator){
wait.until(driver -> {
try {
driver.findElement(locator).click();
return true;
}catch (Exception e){
return false;
}
});
}
  • The “ExpectedConditions” is a predicate within the “wain.until();’’ that returns true or false.
  • In first click() method, “ExpectedConditions” is used
  • In second click() method, “try-catch” is used within the “lambda” method, so unti l“driver.findElement” will return true within the given amount of time, this process will repeat itself.

--

--

Hasan Ozyer

Junior Test Automation Engineer. Documenting my IT journey for my future self. Sharing my opinions, learnings, and adventures in the tech world.