Check for element to be clickable without an explicit wait time

1.5k Views Asked by At

As stated by the Selenium Documentation we never should mix up explicit and implicit wait times:

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

I've set a implicit wait time of 5000 ms. At the end of some Browser interaction I just want to verify whether the required links are clickable.

I know that this can be done by using ExpectedConditions, but this implies an explicit wait time as in the example below.

protected PageNewDocument isElementClickable(WebElement element)
{
    (new WebDriverWait(driver, 1)).until(ExpectedConditions.elementToBeClickable(element));
    return this;
}

How can I check for elements to be clickable without the definition of an explicit wait time?

1

There are 1 best solutions below

1
Cathal On

Answered a similar question here the other day. This method waits for the page to be loaded before returning true. So your elements should be clickable then.

private static WebDriverWait wait = new WebDriverWait(driver, 60);
private static JavascriptExecutor js  = (JavascriptExecutor) driver;

public static void waitForPageLoaded() {
            wait.until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver driver) {
                    Boolean res = (js.executeScript("return document.readyState").equals("complete"));
                    System.out.println("[DEBUG] waitForPageLoaded: " + res);
                    return res;
            }
    });
}