How to check that the element is clickable or not in Java Selenium WebDriver?
Ensuring that an element is clickable in your Selenium WebDriver tests is crucial for validating the functionality and user experience of your web applications. In Java Selenium WebDriver, checking if an element is clickable before interacting with it can help avoid errors and ensure that your test scripts run smoothly.
This guide will walk you through the steps to determine whether an element is clickable using WebDriverWait and ExpectedConditions, providing you with practical examples to enhance your test automation.
Example to check that the element is clickable or not in Java Selenium WebDriver
To check if an element is clickable, you can use the WebDriverWait
class combined with ExpectedConditions
provided by Selenium. This approach allows you to wait for the element to become clickable before attempting any interaction. Below are two practical examples to help you understand how to implement this in your Selenium test scripts.
package seleniumpractice;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import io.github.bonigarcia.wdm.WebDriverManager;
import java.time.Duration;
public class ClickableElement {
public static void main(String[] args) {
// Setup WebDriverManager for ChromeDriver
WebDriverManager.chromedriver().setup();
// Create a new instance of the Chrome driver
WebDriver driver = new ChromeDriver();
try {
// Navigate to the Dynamic Controls page
driver.get("http://the-internet.herokuapp.com/dynamic_controls");
// Create a WebDriverWait instance with Duration
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); // Timeout in seconds
// Define the locator for the "Enable" button
By enableButtonLocator = By.xpath("//button[text()='Enable']");
try {
// Wait until the "Enable" button is clickable
WebElement enableButton = wait.until(ExpectedConditions.elementToBeClickable(enableButtonLocator));
// Click the "Enable" button
enableButton.click();
// If we reached this point, the button was clickable and clicked
System.out.println("The 'Enable' button was clickable and has been clicked.");
// Optionally, wait for the text box to become enabled
By textBoxLocator = By.id("input-example");
WebElement textBox = wait.until(ExpectedConditions.elementToBeClickable(textBoxLocator));
System.out.println("The text box is now enabled.");
} catch (Exception e) {
// Handle the case where the button is not clickable
System.out.println("The 'Enable' button is not clickable or an error occurred.");
}
} finally {
// Close the browser
driver.quit();
}
}
}
Output
When you run the above code examples, the expected output will vary based on whether the element is clickable or not. Here’s what you can expect:
- If the element is clickable: You will see the message "Element is clickable and has been clicked."
- If the element is not clickable: You will receive an error message indicating why the element could not be clicked, such as "Element is not clickable: [exception message]."

Conclusion
In Java Selenium WebDriver, verifying that an element is clickable is essential for reliable web automation testing. By utilizing WebDriverWait and ExpectedConditions, you can effectively check if an element is clickable before attempting any actions. This approach helps prevent common issues like ElementClickInterceptedException
and ensures that your test scripts are robust and dependable. Implement these practices to improve the accuracy and stability of your automated tests.