I came across a piece of code in an existing Selenium automation framework that checks for the width and height of an element before determining if it is interactable. It seems like the element can only be clicked or double-clicked if both its width and height have positive values.
public boolean isElementInteractable(WebElement element) {
JavascriptExecutor js = (JavascriptExecutor) driver;
String offsetWidth = js.executeScript("return arguments[0].offsetWidth;", element).toString();
String offsetHeight = js.executeScript("return arguments[0].offsetHeight;", element).toString();
if ((Integer.parseInt(offsetWidth) != 0 && Integer.parseInt(offsetHeight) != 0) && element.isDisplayed()) {
return true;
}
return false;
}
Based on my understanding, any visible or interactable element should already have a positive height and width. I find it puzzling why this specific code was implemented in the first place.
I would appreciate it if you could validate my understanding. If I am mistaken, please provide insight into scenarios where this approach may be necessary.