Search code examples
seleniumautomationnightwatch.js

Nightwatch - Node.js - How to remove/delete HTML elements?


Apparently, in Nightwatch, there's no 'document' or 'window' object. There is a 'browser' object which seems to be similar.

Tried using .getElementById(), .remove(), .removeChild() and Nightwatch doesn't recognize the methods.


Solution

  • You have to use .execute() function to write document statements. In the below example I am using the querySelector to find the element and then asserting its innertext in the callback.

    'Nightwatch JS Example': function(browser) {
        browser.url('https://example.com').waitForElementVisible('body').execute(function() {
            return document.querySelector(selector).innerText 
        }, [], function(result) {
            this.assert.equal(result.value, 'Some text')
        })
    }