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
- Pass
ExpectedConditions.elementToBeClickable()with the button's id towait.until()and keep the returned element. - 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. - Click the returned element.
- 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 abooleandoes 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.
Related lessons
- Click a ButtonThe plain
click()this wait protects. - isEnabled() - Check Enabled StateThe enabled check that this condition includes.
- Click Using JavaScriptA JavaScript click, the last resort when an overlay intercepts.
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.
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();
}
}