SELENIUM : JAVA : Check if an element is enabled for interaction on a web page?
SDET Automation Testing Interview Questions & Answers
We will be covering a wide range of topics including QA manual testing, automation testing, Selenium, Java, Jenkins, Cucumber, Maven, and various testing frameworks.
SELENIUM : JAVA : Check if an element is enabled for interaction on a web page?
In Selenium with Java, you can check if an element is enabled for interaction on a web page using the isEnabled() method.
This method returns a boolean value indicating whether the element is currently enabled or not. Here's an example:
// Locating the element
WebElement element = driver.findElement(By.id("myElement"));
// Checking if the element is enabled
if (element.isEnabled()) {
// Element is enabled
// Perform some action on the element
element.click();
} else {
// Element is not enabled
// Take some appropriate action
System.out.println("Element is not enabled");
}
In this example, we first locate the element using its ID (myElement) using the findElement() method. We then use the isEnabled() method to check if the element is enabled or not. If the element is enabled, we can perform some action on it, such as clicking it. Otherwise, we can take some appropriate action, such as printing a message to the console.
Note that an element may be disabled for various reasons, such as being grayed out or having the disabled attribute set. In such cases, the isEnabled() method will return false.