Posted on Leave a comment

How to resolve ElementNotInteractableException: Element is not visible in Selenium webdriver

ElementNotInteractableException is one of the few notable exceptions encountered in the Selenium WebDriver. This W3C exception is thrown to outline how, regardless of the presence of an element in the HTML DOM, it still can’t be interacted with. This is evident in the inability to click or send keys, functions which make running the Selenium WebDriver possible. There are many root causes which may be attached to this problem; the prominent ones are element invisibility or non-display and hidden element behind another. Through this article, we will outline the few steps you can take to rectify this issue.

Temporary Overlay of other WebElement over the WebElement of our interest

When this problem has been identified as the root cause, the most effective way to resolve this is to execute ExplicitWait by combining WebDriverWait and ExpectedCondition as invisibilityOfElementLocated. A sample of this is analyzed below:

WebDriverWait wait2 = new WebDriverWait(driver, 10);
wait2.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("xpath_of_element_to_be_invisible")));
driver.findElement(By.xpath("xpath_element_to_be_clicked")).click();

Another way to go about solving this problem is by approaching it on a more granular level by using ExpectedCondition as elementToBeClickable as opposed to using ExpectedCondition as invisibilityOfElementLocated. This is analyzed below:

WebDriverWait wait1 = new WebDriverWait(driver, 10);
WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_element_to_be_clicked")));
element1.click();

Permanent Overlay of other WebElement over the WebElement of our interest

The best solution for this type of problem is by casting the WebDriver instance as if the overlay is a permanent one. In this case, we have to cast the WebDriver instance as JavascriptExecutor followed by a click operation. This step is analyzed below:

WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);

Other general ways of solving the ElementNotInteractableException include:

  • Waiting until an element is visible/clickable
WebDriverWait wait = new WebDriverWait(driver, timeout);
wait.until(ExpectedConditions.visibilityof(element));
wait.until(ExpectedConditions.elementTobBeClickable(element));
  • Scroll until the element is within the display
Actions action = new Actions(driver):
action.moveToElement(element);
  • Utilizing JS Executor to interact with the DOM
JavascriptExecutor javascript = (JavascriptExecutor) driver;
javascript.executeScript("var element = document.querySelector('locator');element.value = 'whatever';")