Implicit, Explicit & Fluent Wait in Selenium WebDriver

Implicit Wait vs Explicit Wait

Implicit Wait

Implicit Wait will let Selenium WebDriver to wait for a certain amount of time, before throwing the exception that it cannot find the element on the web page.

Note: The Implicit Wait will be in place, when the browser is opened. This means, that any search for elements on the page could take the time the Implicit Wait is set for.

WebDriver driver = new FirefoxDriver();

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

driver.get("myURL");

WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));

Explicit Wait

It is more extendable in the means that you can set it up to wait for any condition you might like. Usually, you can use some of the pre-built Expected Conditions to wait for elements to become clickable, visible, invisible, etc.

driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //nullify implicitlyWait()

WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);

element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));

Fluent Wait

Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions while waiting, such as NoSuchElementExceptions when searching for an element on the page.

FluentWait  wait = new FluentWait(driver).withTimeout(timeOutInSeconds,TimeUnit.SECONDS)
           .pollingEvery(200,TimeUnit.MILLISECONDS)
           .ignoring(NoSuchElementException.class);

element = (WebElement) wait.until(ExpectedConditions.visibilityOfElementLocated(by));

 

 

Related Post