How to resolve exception Element Not Interactable Exception in Selenium?

SeleniumAutomation TestingTesting Tools

We can resolve the exception – ElementNotInteractableException with Selenium webdriver. This exception is thrown if a webelement exists in DOM but cannot be accessed. The below image shows an example of such an exception.

If a specific webelement is overspread by another webelement we normally get this exception. To fix this, we can either apply explicit wait so that the webdriver waits for the expected condition - invisibilityOfElementLocated of the overlaying webelement.

Or, we can apply the expected condition - elementToBeClickable on the webelement that we want to interact with. To resolve a permanent overlay, we have to use the JavaScript Executor to perform the click action. Selenium utilizes the executeScript method to run JavaScript commands.

Syntax

WebDriverWait wait= (new WebDriverWait(driver, 5));
wait.until(ExpectedConditions . elementToBeClickable (By.id("element id")));
//alternate solution
wait.until(ExpectedConditions. invisibilityOfElementLocated(By.id("overlay element id")));
//fix with JavaScript executor
WebElement m = driver.findElement(By.id("element id"));
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].click();", m);

Example

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.JavascriptExecutor;
public class InteractableExceptResolve{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      //launch URL
      driver.get("https://login.yahoo.com/");
      //identify element
      WebElement m = driver.findElement(By.xpath("//input[@id='persistent']"));
      //JavascriptExecutor to click element
      JavascriptExecutor jse = (JavascriptExecutor) driver;
      jse.executeScript("arguments[0].click();", m);
      boolean b = m.isSelected();
      if (b) {
         System.out.println("Checkbox is not checked");
      }else {
         System.out.println("Checkbox is checked");
      }
      driver.close();
   }
}

Output

raja
Published on 03-Apr-2021 10:15:34
Advertisements