There is a common method to test if a WebElement is clickable:
It typically involves something like this:
public static boolean isElementClickable(WebDriver driver, String accessor){
return driver.findElements(By.xpath(accessor)).size() > 0 && driver.findElement(By.xpath(accessor)).isDisplayed() && driver.findElement(By.xpath(accessor)).isEnabled();
//isDisplayed(): This method helps avoid the need to parse an element's "style" attribute to check its visibility. It returns false when the element is not present
//isEnabled(): Generally returns true, except for disabled input elements.
}
This function has a flaw because it only checks if the Element is clickable at the DOM level. If the element is hidden or overlapped due to CSS issues, one may encounter an Exception like:
org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (781, 704). Other element would receive the click:
...
In such cases, one can still interact with the element using:
// Assume 'driver' is a valid WebDriver instance that
// has been properly instantiated elsewhere.
WebElement element = driver.findElement(By.id("example"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
However, I am curious about how we can determine without actually clicking through executor.executeScript
whether a WebElement is visible and not obstructed by another element, ensuring it is perfectly clickable.
If anyone could provide insight on this matter, I would greatly appreciate it as my research has not yielded any definitive answers thus far.