The ElementNotInteractableException in Selenium occurs when an element is hidden behind another element, not yet visible, or disabled.
To handle this exception effectively, we should use explicit waits, which allow Selenium to wait until a specific condition (such as element visibility or clickability) is met before interacting with the element.
It’s generally better to avoid relying solely on implicit waits, since they may cause Selenium to attempt actions before the page or elements are fully loaded, leading to inconsistent behavior.
Below are example scripts demonstrating how to handle ElementNotInteractableException using explicit waits in Java, C#, and JavaScript:
Example Script
-
Java
-
C#
-
JavaScript
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 org.openqa.selenium.ElementNotInteractableException;
import java.time.Duration;
public class HandleElementNotInteractable {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
try {
// Wait until the element is clickable
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("submitBtn")));
button.click();
System.out.println("Button clicked successfully!");
} catch (ElementNotInteractableException e) {
System.out.println("Element not interactable. Trying alternative...");
// Retry logic or fallback action
} finally {
driver.quit();
}
}
}
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using SeleniumExtras.WaitHelpers;
using System;
class HandleElementNotInteractable {
static void Main() {
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://example.com");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
try {
// Wait until the element is clickable
IWebElement button = wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("submitBtn")));
button.Click();
Console.WriteLine("Button clicked successfully!");
} catch (ElementNotInteractableException e) {
Console.WriteLine("Element not interactable. Handling gracefully...");
// Retry logic or fallback action
} finally {
driver.Quit();
}
}
}
const { Builder, By, until } = require("selenium-webdriver");
(async function handleElementNotInteractable() {
let driver = await new Builder().forBrowser("chrome").build();
try {
await driver.get("https://example.com");
try {
// Wait until element is clickable
let button = await driver.wait(until.elementLocated(By.id("submitBtn")), 10000);
await driver.wait(until.elementIsVisible(button), 10000);
await driver.wait(until.elementIsEnabled(button), 10000);
await button.click();
console.log("Button clicked successfully!");
} catch (err) {
if (err.name === "ElementNotInteractableError") {
console.log("Element not interactable. Handling gracefully...");
// Retry logic or fallback action
} else {
throw err; // rethrow if it's not the exception we expect
}
}
} finally {
await driver.quit();
}
})();