Search code examples
node.jsnightwatch.js

Can't click on element in modal using Nightwatch


I am writing some tests using Nightwatch, and I am at the very end of my test and I have 1 issue I can't figure out. I have a button to submit something, and then when they click submit, a modal pops up asking them if they are sure. I am trying to click "Yes, I am sure" but Nightwatch isn't finding the element. Below is my code.

Click on Submit Button (This works)

.getLocationInView('#objective-content-intro > div > div > div > div:nth-child(4) > div > div:nth-child(8) > div > div > a')
.assert.visible('#objective-content-intro > div > div > div > div:nth-child(4) > div > div:nth-child(8) > div > div > a')
.click('#objective-content-intro > div > div > div > div:nth-child(4) > div > div:nth-child(8) > div > div > a')

Click on "Yes, I'm sure" button (This doesn't work)

.getLocationInView('body > div.sweet-alert.hiddenField.showSweetAlert.visible > div.sa-button-container > div > button')
.assert.visible('body > div.sweet-alert.hiddenField.showSweetAlert.visible > div.sa-button-container > div > button')
.waitForElementPresent('body > div.sweet-alert.hiddenField.showSweetAlert.visible > div.sa-button-container > div > button', 3000, 'Click on \"Yes, submit it!\"')
.pause(3000)
.click('body > div.sweet-alert.hiddenField.showSweetAlert.visible > div.sa-button-container > div > button')

I just get an error saying it is unable to find the element.


Solution

  • Try this

    .waitForElementPresent('#location > of > modal',20000, 'Some message here to show while running test')
    .pause(1000)
    .execute(function(){
        document.querySelector('body > div.sweet-alert.hiddenField.showSweetAlert.visible > div.sa-button-container > div > button').click()
    });