Skip to content
CodeKruPro

Explicit Wait - Element Clickable

37/70

Explicit Wait - elementToBeClickable

elementToBeClickable waits until an element is both visible and enabled (clickable). This is useful for buttons that may take time to become active.

Task:

  1. Navigate to https://testkru.com/Elements/Buttons
  2. Use WebDriverWait to wait for the leftClick button to be clickable
  3. Click the button
  4. Print the button text after clicking

Expected output:

Button is clickable: true
After click: I was left-clicked!

About this lesson

elementToBeClickable in Selenium Java: wait for a button before clicking

Learn Selenium JavaIntermediateWaits

How it works

ExpectedConditions.elementToBeClickable() holds when the element is visible and enabled, and until() then returns the element. It is the usual wait before a click() on a button that starts disabled, such as Submit before a form is valid.

It does not check whether something covers the element. If a spinner overlay or a cookie banner sits on top, the wait still succeeds and the click() that follows throws ElementClickInterceptedException. For that case, wait for the overlay to disappear first, as lesson 38 does.

When the element stays disabled the wait fails with a specific message. On the Buttons page, waiting two seconds for disabledButton throws TimeoutException with "to be clickable, but the element was not enabled".

About the assignment

The starter creates a ten-second wait for you. You wait until the left-click button is ready, report that the wait succeeded, click the button it returned, and print the label the click produces.

What you will do

  1. Pass ExpectedConditions.elementToBeClickable() with the button's id to wait.until() and keep the returned element.
  2. Print that the button is clickable after the label Button is clickable:. If the wait had failed, it would have thrown, so reaching this line is the proof.
  3. Click the returned element.
  4. Print its text after the label After click:.

Common mistakes

  • Finding the element again after the wait

    It works, but it is a second lookup for something you already have. Use the element until() returned.

  • Expecting a boolean from until()

    This condition returns a WebElement, so assigning the result to a boolean does not compile.

  • Treating clickable as unobstructed

    An element under an overlay still passes this wait, and the click then throws ElementClickInterceptedException.

In a real test suite

Forms that validate as you type, payment buttons that wait for a provider to load, and wizards whose Next button unlocks after a step are all places where this wait sits right before a click.

Lesson 36 waited for visibility. Lesson 38 turns the idea around and waits for an element to go away.

To try one of these calls on any page without a task, open the Online Selenium Java Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import java.time.Duration;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/Buttons");
        
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        
        // TODO: Wait for leftClick to be clickable
        // TODO: Print that it's clickable
        // TODO: Click it and print text after
        
        driver.quit();
    }
}