Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. QA Testing
  3. Question
QA Testing

How can I solve the exception - Selenium element not interactable?

Asked by Angela Baker May 9, 2022 4.5K views 2 answers
Share

About this question

driver.FindElement(By.Id("UserName")).SendKeys("test");
driver.FindElement(By.Id("Password")).SendKeys("test123");       
driver.FindElement(By.XPath("//*[@id="btnSubmit"]")).Click();

Also I'm using ImplicitWait command, thread and still facing the issue

Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Jul 5, 2024

The ElementNotInteractableException in Selenium typically occurs when an element is present in the DOM but is not in a state that can be interacted with. This can happen for several reasons, such as the element being hidden, not yet rendered, or overlapping with another element. Here are some strategies to resolve this issue:

1. Wait for the Element to be Interactable

Ensure that the element is ready for interaction. Use explicit waits to wait for conditions such as visibility or clickability.

Example using WebDriverWait:

  import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.WebDriverWait;WebDriver driver = // initialize your driverWebDriverWait wait = new WebDriverWait(driver, 10);WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("elementId")));element.click();

2. Check Element Visibility

Ensure that the element is visible on the page before interacting with it.

Example using visibilityOfElementLocated:

  WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elementId")));element.click();

3. Scroll to the Element

Sometimes elements are not interactable because they are outside the visible viewport. Scrolling the element into view can help.

Example using JavaScriptExecutor:

  import org.openqa.selenium.JavascriptExecutor;WebElement element = driver.findElement(By.id("elementId"));JavascriptExecutor js = (JavascriptExecutor) driver;js.executeScript("arguments[0].scrollIntoView(true);", element);element.click();

4. Ensure the Element is Enabled

Ensure that the element is not disabled.

Example:

  WebElement element = driver.findElement(By.id("elementId"));if (element.isEnabled()) {    element.click();} else {    // handle the case where the element is disabled}

5. Handle Overlapping Elements

Sometimes other elements may overlap the target element, preventing interaction. In such cases, you might need to handle or close the overlapping elements first.

Example using Action class:

  import org.openqa.selenium.interactions.Actions;Actions actions = new Actions(driver);WebElement element = driver.findElement(By.id("elementId"));actions.moveToElement(element).click().perform();

6. Debugging and Additional Checks

Add debugging statements to confirm the element’s state and visibility.

Example:

  WebElement element = driver.findElement(By.id("elementId"));System.out.println("Element is displayed: " + element.isDisplayed());System.out.println("Element is enabled: " + element.isEnabled());

7. Alternative Interactions

If clicking on the element is not possible, consider alternative interactions such as sending keys to the element or using JavaScript to perform the click.

Example using JavaScript for click:

  JavascriptExecutor js = (JavascriptExecutor) driver;js.executeScript("arguments[0].click();", element);

Summary

To solve the ElementNotInteractableException in Selenium:

Wait for the Element to be Interactable: Use explicit waits like WebDriverWait to wait until the element is clickable or visible.

Check Element Visibility: Ensure the element is visible before attempting interaction.

Scroll to the Element: Use JavaScript to scroll the element into view.

Ensure the Element is Enabled: Check that the element is enabled.

Handle Overlapping Elements: Use actions to move to the element or close any overlapping elements.

Debugging and Additional Checks: Add debugging statements to confirm the element’s state.

Alternative Interactions: Use JavaScript to interact with the element if necessary.

By applying these techniques, you can handle most scenarios where an element is not interactable in Selenium.








Was this helpful?

More QA Testing discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest QA Testing Blogs

Guides, tips and career advice on QA Testing from JanBask experts.