Understanding Element Not Interactable Exception in Selenium | BrowserStack (2024)

Selenium is the most widely used automation tool to automate web applications using various programming languages such as Java, Python, and JavaScript. As an automation tester you would have come across an exception called ElementNotInteractableException very often while automating web applications.

This article discusses in which circ*mstances this exception occurs and what are the different ways to solve the exception.

What is an ElementNotInteractable Exception?

ElementNotInteractableException is thrown by Selenium WebDriver when the element is present in the DOM but cannot be interacted with. This includes an element that is not displayed or whose center point cannot be scrolled into the viewport. The element is not interactable in the sense it cannot be clicked or send keys cannot work as the element is:

  • not visible or hidden
  • disabled
  • outside the viewport and inaccessible
  • overlapped by another element
  • not completely rendered on the page

Table of Contents

  • When does an ElementNotInteractable Exception occur?
  • How to handle ElementNotInteractable Exception in Selenium
    • 1. Wait for the element to be visible
    • 2. Scroll into the View
    • 3. Enable the web element
    • 4. Handle overlapping elements
    • 5. Handle switching to correct frame
  • Why is testing on real devices and browsers important?
  • Why use BrowserStack Automate for Selenium Tests?
  • When does an ElementNotInteractable Exception occur?

    ElementNotInteractable exception occurs in Selenium when an attempt is made to interact with a web element but the element is not in a condition to be interacted with.

    Here are the possible reasons for this exception to occur:

    1. Element is not visible: If the targeted web element present in the DOM is hidden or covered by another element it makes it non-interactive.
    2. Element is disabled: If the web element is present in the DOM but is disabled, such as disabled button or disabled checkbox or radio button. This element cannot be interacted with until it is enabled.
    3. Element is outside the viewport and inaccessible: When the element is present in the DOM but is not in view, it can be made interactive only after the view is scrolled.
    4. Element is overlapped by another element: If the web element to be interacted with is overlapped by another web element such as modal dialog, it cannot be interacted with until the overlapping element is moved or removed.
    5. Element is not rendered: If the web element is rendered dynamically on the web page and is not fully loaded when the interaction takes place, it cannot be interacted until it is loaded completely.

    How to handle ElementNotInteractable Exception in Selenium

    ElementNotInteractableException can only be handled by making the web element to be interactable. Different approaches should be implemented to make the web element interactable before attempting to interact with it such as scroll until the web element is in view, enable the web element if disabled or apply a retry mechanism.

    Below are some of the common ways to handle this exception.

    1. Wait for the element to be visible

    To make the web element interactable implement explicit wait as it allows to wait for a specific condition to be met before performing the next action. Use WebDriverWait along with ExpectedConditions as below:

    Code snippet:

    public class ElementNotInteractableException {@Testpublic void verifyPageTitle() { // Initialize ChromeDriverWebDriver driver = new ChromeDriver(); // Open the webpagedriver.get("https://www.browserstack.com/"); // Initialize WebDriverWait with a timeout of 10 secondsWebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); // Find the element which may not be initially interactableWebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("signupModalProductButton"))); // Perform action on the elementelement.click();driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(2));Assert.assertEquals(driver.getCurrentUrl(), "https://www.browserstack.com/users/sign_up"); // Close the browserdriver.quit();}}

    In the above program, applying an explicit wait for the “Get Started free” button which is present in the BrowserStack home page.

    2. Scroll into the View

    In Selenium, JavaScriptExecutor is used to scroll the page and make the web element visible within the viewport of the web page. Some webpages are long and dynamic in nature and as it is scrolled, it shows data to the user.

    In such scenarios, if you are trying to interact with a web element which is not in view yet, Selenium throws ElementNotInteractableException. By using JavaScript, you can scroll till the required web element and make it accessible for actionable activities such as clicking, typing, etc.

    You can use the executeScript method of JavaScriptExecutor class to scroll to the element like below.

    WebElement element = driver.findElement(By.id("signupModalProductButton"));// Scroll element into view using JavaScriptExecutor((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);

    3. Enable the web element

    Input elements such as checkboxes, radio buttons and dropdowns are disabled in a web page in some context. Disabled means it is not enabled and any action performed by Selenium on such elements will throw an exception.

    <input type ="checkbox" name ="maths" value ="on" disabled>"Maths"<input type ="checkbox" name ="physics" value ="on">"Physics"

    In the above HTML code, “maths” checkbox is disabled on the web page, whereas the “physics” checkbox is enabled.

    To avoid exception in such scenarios, check for the element’s state and then try to execute the action on it, like below:

    WebElement element = driver.findElement(By.LocatorType("Locator value"));if (element.isEnabled()) {// If the element is enabled, perform desired actionselement.click();} else {// If the element is disabled, handle the situation accordinglySystem.out.println("Element is disabled.");}

    4. Handle overlapping elements

    In some scenarios, the web element to be interacted with is blocked or overlapped by some other elements on the web page. The element such as modal dialog or pop-ups which are blocking the desired web element should be moved or dismissed from the view. Dismissing such overlapping elements is necessary to interact with the desired web element.

    WebElement modalDialogBox = driver.findElement(By.id("modalDialogId"));if (modalDialogBox.isDisplayed()) {// Close the modal dialog driver.findElement(By.id("modalButtonCloseBtn")).click();}

    It is also possible that the element to be interacted with is present in the modal dialog box or alert box. In such cases, switch to the desired web element and then perform the operation.

    5. Handle switching to correct frame

    If the element to be interacted with is inside a frame, you need to switch to that frame using switch method as follows:

    driver.switchTo().frame(frameIndex);driver.switchTo().frame(frameId);driver.switchTo().frame(frameName);

    Use the appropriate method to switch to the desired frame and the web element. Also ensure that before switching to the frame, it is completely loaded.

    This article discussed what is ElementNotInteractableException, how it is surfaced and how you can avoid this exception by implementing various strategies.

    Why is testing on real devices and browsers important?

    Testing on emulators and simulators can be cost effective, nevertheless, it is crucial to test the web applications on real devices as testing on simulators and emulators cannot represent real-world scenarios such as network conditions, hardware configurations and environmental factors.

    BrowserStack is a popular cloud-based platform which provides 3500+ real devices and browsers to test the web applications from anywhere. It provides a pool of products like Automate, Live, Percy, App Live, App Automate, etc to cater different types of testing.

    Talk to an Expert

    Below are some of the features provided by BrowserStack’s Automate product:

    1. Diverse environment testing: With BrowserStack you can automate Selenium tests across different devices, operating systems and browsers.
    2. Real Devices and Browsers: It is always wiser to test the application on real devices and browsers to simulate a real user experience. Testing on real devices gives the actual confidence about the application performance.
    3. Concurrent test execution: Simultaneous test execution of multiple Selenium tests can be achieved which reduces the total execution time, gives quicker feedback and accelerates deployment cycles.
    4. Custom Reports with Artifacts: In Automate, custom reports can be generated to provide detailed and customized reports for the automated test execution.
    5. Easy Integration with CI/CD Pipeline: BrowserStack’s Automate product can be seamlessly integrated with popular CI/CD tools such as Jenkins, TeamCity, TravisCI

    Why use BrowserStack Automate for Selenium Tests?

    Different devices and browsers render web pages differently. To ensure that a given application behaves uniformly across different pools of devices and browsers, it is advisable to test it in the real devices and browsers with a real environment set up.

    Testing in real devices and browsers ensures high-quality user experience, compatibility across different platforms, and validates performance and security of the application in real user conditions. BrowserStack is one such platform which leverages testing any mobile or web application by providing 3500+ real devices.

    Try BrowserStack Automate

    Understanding Element Not Interactable Exception in Selenium | BrowserStack (2024)
    Top Articles
    [OFFICIAL]Dr.Fone Support Center - Install&Uninstall FAQs
    Adam Smith on Money as a Medium of Exchange | Adam Smith Works
    Splunk Stats Count By Hour
    How Much Does Dr Pol Charge To Deliver A Calf
    Algebra Calculator Mathway
    Usborne Links
    South Park Season 26 Kisscartoon
    Robinhood Turbotax Discount 2023
    Devotion Showtimes Near Mjr Universal Grand Cinema 16
    Kristine Leahy Spouse
    THE 10 BEST River Retreats for 2024/2025
    Cosentyx® 75 mg Injektionslösung in einer Fertigspritze - PatientenInfo-Service
    House Share: What we learned living with strangers
    biBERK Business Insurance Provides Essential Insights on Liquor Store Risk Management and Insurance Considerations
    Space Engineers Projector Orientation
    Guilford County | NCpedia
    24 Hour Walmart Detroit Mi
    Eka Vore Portal
    979-200-6466
    Craiglist Kpr
    Ess.compass Associate Login
    Charter Spectrum Store
    Best Mechanics Near You - Brake Masters Auto Repair Shops
    Exl8000 Generator Battery
    Rochester Ny Missed Connections
    University Of Michigan Paging System
    Nk 1399
    800-695-2780
    Xpanas Indo
    Ullu Coupon Code
    Mrstryst
    Clearvue Eye Care Nyc
    Memberweb Bw
    Cars And Trucks Facebook
    Planet Fitness Santa Clarita Photos
    Devotion Showtimes Near The Grand 16 - Pier Park
    “Los nuevos desafíos socioculturales” Identidad, Educación, Mujeres Científicas, Política y Sustentabilidad
    Sunrise Garden Beach Resort - Select Hurghada günstig buchen | billareisen.at
    Tillman Funeral Home Tallahassee
    Telugu Moviez Wap Org
    Craigslist Free Manhattan
    Directions To The Closest Auto Parts Store
    Traumasoft Butler
    Www Craigslist Com Atlanta Ga
    Craigslist Antique
    Thotsbook Com
    Mauston O'reilly's
    Suppress Spell Damage Poe
    German American Bank Owenton Ky
    Acuity Eye Group - La Quinta Photos
    Ics 400 Test Answers 2022
    Gelato 47 Allbud
    Latest Posts
    Article information

    Author: Patricia Veum II

    Last Updated:

    Views: 6017

    Rating: 4.3 / 5 (64 voted)

    Reviews: 87% of readers found this page helpful

    Author information

    Name: Patricia Veum II

    Birthday: 1994-12-16

    Address: 2064 Little Summit, Goldieton, MS 97651-0862

    Phone: +6873952696715

    Job: Principal Officer

    Hobby: Rafting, Cabaret, Candle making, Jigsaw puzzles, Inline skating, Magic, Graffiti

    Introduction: My name is Patricia Veum II, I am a vast, combative, smiling, famous, inexpensive, zealous, sparkling person who loves writing and wants to share my knowledge and understanding with you.