How do I resolve the ElementNotInteractableException in Selenium WebDriver?

SeleniumAutomation TestingTesting Tools

We get the ElementNotInteractableException in Selenium if an element is available in DOM but not in a condition to be interacted. Some of the reasons for this exception are −

There may be a covering of another element on the element with which we want to interact with. This overspread of an element over the other can be temporary or permanent. To resolve a temporary overspread, we can wait for an expected condition for the element.

We can wait for the expected condition of invisibilityOfElementLocated for the overlay element. Or, wait for the expected condition of elementToBeClickable for the element with which we want to interact.

Syntax

WebDriverWait w= (new WebDriverWait(driver, 7));
w.until(ExpectedConditions
. elementToBeClickable (By.id("id of element")));
//alternate solution
w.until(ExpectedConditions
. invisibilityOfElementLocated(By.id("id of overlay element")));

To resolve a permanent overspread, we can use the JavaScript Executor to perform the click action. Selenium uses the executeScript method to execute JavaScript commands.

Syntax

WebElement l = driver.findElement(By.id("id of element"));
JavascriptExecutor j = (JavascriptExecutor) driver;
j.executeScript("arguments[0].click();", l);

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 InteractableExceptionFix{
   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 l = driver.findElement(By.xpath("//input[@id='persistent']"));
      //JavascriptExecutor to click element
      JavascriptExecutor j = (JavascriptExecutor) driver;
      j.executeScript("arguments[0].click();", l);
      boolean t = l.isSelected();
      if (t) {
         System.out.println("Checkbox is not checked");
      }else {
         System.out.println("Checkbox is checked");
      }
      driver.quit();
   }
}

Output

raja
Published on 07-Apr-2021 09:18:18
Advertisements