Selenium: How to use 'Waits' in Selenium Java

Selenium: How to use 'Waits' in Selenium Java

With Selenium, there are three ways of waiting for elements: explicit wait, implicit wait and fluent wait. The following article shows three test examples, each one showing one of the above waits in action.

'Explicit Wait' waits until either a certain condition is met or the timeout has been reached before attempting to return an element.

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement ele = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[strong[.='English']]")));
 

'Implicit Wait' will simply wait for a specified amount of time, returning the element if and when it is found.

     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    WebElement ele = driver.findElement(By.xpath("//a[strong[.='English']]"));

'Fluent Wait' is similar to explicit wait except it is more flexible in that the timing of when the page is checked can be altered and also can be told to ignore certain errors while it is waiting.

    Wait wait = new FluentWait(driver)
            .withTimeout(30, TimeUnit.SECONDS)
            .pollingEvery(5, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class);

    WebElement ele = wait.until(new Function() {

Leave a reply

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

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.