How can the "Element Not Interactable Exception" in Selenium be handled while performing web application automation?
In the following Selenium code, I am not able to send keys to the password field. I have clicked on the box, cleared it, and sent the keys but none if the options have worked. But when I am debugging the code and running it step by step it works fine as expected.
Here’s the code:
public class TestMail {
protected static WebDriver driver;
protected static String result;
@BeforeClass
public static void setup() {
System.setProperty("webdriver.gecko.driver", "D:\geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}
@Test
void Testcase1() {
driver.get("http://mail.google.com");
WebElement loginfield = driver.findElement(By.name("Email"));
if (loginfield.isDisplayed()) {
loginfield.sendKeys("ragesh@gmail.in");
} else {
WebElement newloginfield = driver.findElement(By.cssSelector("#identifierId"));
newloginfield.sendKeys("ragesh@gmail.in");
}
driver.findElement(By.name("signIn")).click();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
WebElement pwd = driver.findElement(By.cssSelector("#Passwd"));
pwd.click();
pwd.clear();
if (pwd.isEnabled()) {
pwd.sendKeys("123");
} else {
System.out.println("Not Enabled");
}
}
}
Possible Causes and Solutions
Dynamic Content or Page Load Timing
The password field might not be interactable because the page or some elements are still loading.
Solution: Use an explicit wait to ensure the element is ready for interaction:
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.By;
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement pwd = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#Passwd")));
pwd.click();
pwd.clear();
pwd.sendKeys("123");
Element Overlapping or Obstruction
Another element (e.g., a popup, tooltip, or animation) might be covering the password field.
Solution: Scroll the element into view before interacting with it:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", pwd);
pwd.click();
pwd.clear();
pwd.sendKeys("123");
Element Hidden or Not Yet Rendered
The password field might not be visible or fully rendered when your script tries to interact with it.
Solution: Wait for the element to become visible:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement pwd = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#Passwd")));
pwd.click();
pwd.clear();
pwd.sendKeys("123");
Incorrect Locator
Verify if #Passwd is the correct CSS selector. Google login forms often use dynamic IDs and classes, which can change.
Solution: Inspect the DOM and use a robust locator (e.g., based on attributes, name, or a stable class). For instance:
WebElement pwd = driver.findElement(By.name("password"));
pwd.click();
pwd.clear();
pwd.sendKeys("123");
Shadow DOM
Some modern web pages use Shadow DOM, and Selenium cannot interact with shadow elements directly.
Solution: Use JavaScript to interact with elements inside the Shadow DOM:
WebElement shadowHost = driver.findElement(By.cssSelector("shadow-host-selector"));
WebElement shadowRoot = (WebElement) ((JavascriptExecutor) driver)
.executeScript("return arguments[0].shadowRoot", shadowHost);
WebElement pwd = shadowRoot.findElement(By.cssSelector("#Passwd"));
pwd.click();
pwd.clear();
pwd.sendKeys("123");
Debug Mode Success
If debugging works but running does not, it indicates a timing issue.
Solution: Add explicit waits as shown above and ensure animations or transitions are complete.
JavaScript Click and Send Keys
As a last resort, use JavaScript for interaction:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", pwd);
js.executeScript("arguments[0].value='123';", pwd);
Final Optimized Code
Here’s an optimized version of your code that incorporates explicit waits and robust locators:
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.concurrent.TimeUnit;
public class TestMail {
protected static WebDriver driver;
public static void setup() {
System.setProperty("webdriver.gecko.driver", "D:\geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}
public static void Testcase1() {
driver.get("http://mail.google.com");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement loginfield = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("Email")));
loginfield.sendKeys("ragesh@gmail.in");
driver.findElement(By.name("signIn")).click();
WebElement pwd = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#Passwd")));
pwd.click();
pwd.clear();
pwd.sendKeys("123");
}
public static void main(String[] args) {
setup();
Testcase1();
driver.quit();
}
}
Debugging Tips
Take Screenshots: Capture the state of the page when the exception occurs:
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("screenshot.png"));
Verify Element Properties: Use the browser developer tools to check if the password field is visible, enabled, and correctly located.
If the issue persists, share additional details, and I’ll assist further!