How to resolve element not interactable exception in selenium

How to resolve element not interactable exception in selenium

When using Selenium test automation, users may encounter an issue where an selenium element not interactable. This can be a frustrating issue to deal with as it can be difficult to determine the root cause. We have often faced this issue when automating a web application using selenium and this issue occurs when the script is able to recognize the element but it is not in interactable mode for further actions.

How to troubleshoot this issue?

The first step in troubleshooting this issue is to determine if the element is actually visible on the page. If the element is not visible, then the issue may be that the element is hidden or not loaded yet. If the element is visible, then the issue may be that it is not interactable.

In some cases, the element may be interactable but the Selenium test automation is unable to interact with it. This could be due to timing issues or compatibility issues between the Selenium test automation and the application. If the issue is related to timing, then the user may need to add additional wait time for the element to become interactable. This can be done by adding an explicit wait or an implicit wait before interacting with the element.

//Explicit wait when element is not interactable 

WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("element_id"))); //Perform action element.click();

Below we have shown many more ways to resolve this issue ūüĎć

1. Check if the element is within the visible viewport: Make sure the element is within the visible viewport. If it isn't, you'll need to scroll to the element before attempting to interact with it. 

//Using JavaScriptExecutor 

WebElement element = driver.findElement(By.id("example")); 

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

//Using Actions class 

WebElement element = driver.findElement(By.id("example")); 

Actions actions = new Actions(driver); actions.moveToElement(element); actions.perform();

2. Check if the element is hidden: Check if the element is hidden through CSS or if it has been disabled using JavaScript. If it is, you'll need to make sure it's visible/enabled before attempting to interact with it. 

3.Another workaround is to use Selenium's Action Chains feature. Action Chains can be used to move the mouse over an element that is hidden, which will cause the element to become visible. This can then be used to interact with the element.

Actions act = new Actions(driver);

act.moveToElement(driver.findElement(By.partialLinkText("Flights")));

Thread.sleep(2000);
act.click().build().perform();

 

4. Check if the element is overlapped by another element: Check if the element is overlapped by another element. If it is, you'll need to move the overlapped element before attempting to interact with it. 

You need to use JavaScript to workaround this issue. 

WebElement ws = driver.findElement(By.id("confirmButton"));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click()",ws );

For example, you can use the following code to access an element that is overlapped by another element:

 // Get the element which is overlapped by another element let element = document.querySelector('#overlapped-element'); 

// Move the element to the top-most layer element.style.zIndex = '9999'; 

// Interact with the element element.click();

Back to blog