GLK
GLK

Reputation: 1045

Selenium - element not intractable error - how to click button inside bootstrap modal using JavaScript

I have a simple page with a button, to open bootstrap modal. Though modal is opened, its not able to click button inside modal.

I tried 3 approach to resolve the issue 1) finding element directly 2) finding element through modal element 3) using explicit wait. And in all these approach getting same error - ElementNotInteractableError: element not interactable

It may be be duplicate of some existing answer on stackoverflow ( thats why I could try 3 approaches ), but it didn't help

error stacktrace

DevTools listening on ws://127.0.0.1:50284/devtools/browser/1a87603d-af78-486d-bc37-161eeeed16ba [5396:11184:0428/110516.874:ERROR:browser_switcher_service.cc(238)] XXX Init() ElementNotInteractableError: element not interactable (Session info: chrome=81.0.4044.122) at Object.throwDecodedError (D:\ip300-gk\node_modules\selenium-webdriver\lib\error.js:550:15) at parseHttpResponse (D:\ip300-gk\node_modules\selenium-webdriver\lib\http.js:565:13) at Executor.execute (D:\ip300-gk\node_modules\selenium-webdriver\lib\http.js:491:26) at processTicksAndRejections (internal/process/task_queues.js:93:5) at async Driver.execute (D:\ip300-gk\node_modules\selenium-webdriver\lib\webdriver.js:700:17) at async uitest (D:\ip300-gk\Samples\bootstrap\bs-modal-selenium\uitest.js:26:11) { name: 'ElementNotInteractableError',

Javascript test code, with all 3 approach tried

const driver = require('selenium-webdriver')
const assert = require('assert').strict;
const {Builder, By, Key, until} = require('selenium-webdriver');

let fileName = "D:\\ip300-gk\\Samples\\bootstrap\\bs-modal-selenium\\index.html"
uitest()


async function uitest() {
    let driver = await new Builder().forBrowser('chrome').build();
    let modal, element


    try {
        await driver.get(fileName)
        await driver.findElement(By.id('launchModalButton')).click()

        // approach 1 - getting error - ElementNotInteractableError: element not interactable
        // await driver.findElement(By.id('saveChangesButton')).click()

        // approach 2 using explicit wait - getting error -  ElementNotInteractableError: element not interactable
          element = await driver.wait(until.elementLocated(By.id('saveChangesButton')))
          await element.click()

        // approach 3 - finding modal using root modal - getting error - //ElementNotInteractableError: element not interactable
        //   modal = await driver.findElement(By.id('exampleModal'))
        //   await modal.findElement(By.id('saveChangesButton')).click()


    } catch (err) {
        console.log(err)
    } finally {
        await driver.quit();
    }
}

Bootstrap html page

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
          integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <title>Selenium </title>
</head>
<body>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
        integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
        crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"
        integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
        crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"
        integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
        crossorigin="anonymous"></script>


<div class="container">
    <button type="button" id="launchModalButton" class="btn btn-primary mt-5" data-toggle="modal" data-target="#exampleModal">
        Launch modal
    </button>

    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
         aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    Sample Modal
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button id="saveChangesButton" type="button" class="btn btn-primary">Save changes</button>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>

Upvotes: 0

Views: 1680

Answers (2)

GLK
GLK

Reputation: 1045

Looks like modal is not loaded, before selenium could find button inside and click it. Implicit wait after launching modal did resolve the issue.

await driver.findElement(By.id('launchModalButton')).click()
await driver.manage().setTimeouts( { implicit: 1000} )

Upvotes: 0

debugger89
debugger89

Reputation: 2776

Seems like there is an overlay on top of the button or the button is hidden from the view when you execute the test. You can try with a javascript level click command instead of the default click.

element = await driver.wait(until.elementLocated(By.id('saveChangesButton')))
driver.executeScript("arguments[0].click();", element)

Upvotes: 1

Related Questions