What is Explicit wait

Posted on

Explicit wait is another type of wait mechanism in Selenium WebDriver, where you wait for a certain condition to be met before proceeding to the next steps of the test script. Explicit wait allows you to define a custom condition and a maximum waiting time for that condition to be met.
Explicit wait is useful when you need to wait for a specific condition that is not covered by the implicit wait, or when you want to set a custom wait time for a specific element. It can also be used in combination with implicit wait to ensure that your test scripts wait only as long as it takes for the elements to become available before performing actions on them.
The polling time of an explicit wait in Selenium is the interval at which WebDriver checks whether the expected condition is met. By default, the polling time is set to 500 milliseconds, which means that WebDriver checks for the expected condition every half second. Adjusting the polling time can be useful when testing web pages that load slowly or when it takes longer for the expected condition to be met. However, if you set a query time that is too long, the execution time of the test may increase. Therefore, it is important to strike a balance between waiting for the condition to be met and waiting for the condition to be met without wasting too much time.
Java
Copy
// Wait for an element to be clickable for a maximum of 30 seconds 
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
//With a polling interval of 5 second wait.pollingEvery(Duration.ofSeconds(5));
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("elementID"))); 
// Perform an action on the element element.click();