ElementNotInteractableException – element not interactable

ElementNotInteractableException – A very common exception which occurs in Selenium WebDriver scripts. We will replicate the same scenario and then will solve it.

Scenario

  1. Open Google.
  2. Type “Facebook” in search box.
  3. Click on first result from auto suggestion list.
  4. Verify title of page.

Let’s write an XPath for first option from auto suggestion as –

Simple steps and below is code :-

package BasicSeleniumConcepts;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.ElementClickInterceptedException;
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.testng.Assert;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ClickInterceptedException {

	@Test
	public void googleSearch() {
		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();
		driver.get("https://www.google.com");
		// Typing search keyword
		driver.findElement(By.name("q")).sendKeys("facebook");
		// Locating first suggestion button and click on it. Why first suggestion bcz whatever we type hat will come always at first
		WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
		WebElement ele = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("(//div[@role = 'option']//span)[1]")));
		ele.click();
		Assert.assertTrue(ExpectedConditions.titleContains("facebook").apply(driver).booleanValue());
	}
}

Output

When people get this exception then they will start experimenting by putting some wait or wait for clickable or JavaScript click. In this scenario only JavaScript click may work as it directly works at DOM level but we bypassed the actual reason.

Let me explain the reason behind occurrence of this exception.

As per official doc –

ElementNotInteractableException is Thrown to indicate that although an element is present on the DOM, it is not in a state that can be interacted with.

If you see the XPath of first option from auto suggestions, we are instructing WebDriver to perform Click on a span tag. But is span tag a clickable element above? Answer is No for above scenario and we ended up with ElementNotInteractableException. Please note a span or div can be made clickable.

Observe DOM. Tag “li” is clickable above not tag “span“. WebDriver can perform operation on an element if element is in state that can be interacted.

Let’s correct our XPath which locates li tag instead of span.

package BasicSeleniumConcepts;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.ElementClickInterceptedException;
import org.openqa.selenium.ElementNotInteractableException;
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.testng.Assert;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ClickInterceptedException {

	@Test
	public void googleSearch() {
		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();
		driver.get("https://www.google.com");
		// Typing search keyword
		driver.findElement(By.name("q")).sendKeys("facebook");
		// Locating first suggestion button and click on it. Why first suggestion bcz whatever we type hat will come always at first
		WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
		WebElement ele = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("(//ul[@role='listbox']/li)[1]")));
		ele.click();
		Assert.assertTrue(ExpectedConditions.titleContains("facebook").apply(driver).booleanValue());
		
	}
}

This time scripts run successfully.

This exception may occur in many scenarios when an element is disabled or element is nested. All you need to observe your DOM well so that you can catch root cause instead of trying some workarounds blindly.

You can download/clone above sample project from here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related post here, all API manual and automation related posts here and find frequently asked Java Programs here.

Many other topics you can navigate through menu.

3 thoughts on “ElementNotInteractableException – element not interactable

  1. I need to work with a webelement which is hidden inside a form tag like this where in i have to pass a value. I am getting ElementNotInteractableException. How can we handle this kind of scenario other than correcting xpath ?

    Thanks !

  2. Thank you very much Amod, it quite simple to understand this exception along with an example scenario.

Leave a Reply

Your email address will not be published. Required fields are marked *