As part of my nightwatchjs testing, what I would like to do is get the attribute of an element, then set this attribute value to a different value.
Here is the HTML snippet;
So first of all I'd like to get the value of the data-page-view-threshold attribute, which should be '3' and output this so I know I'm interacting with the correct element.
Currently, my code looks like this;
module.exports = {
'Navigate to an homepage': function (browser, document) {
browser.url(browser.launch_url);
browser.pause(5000);
browser.execute(function() {
return document.getElementById('#newsletterSignup').getAttribute('data-page-view-threshold');
}, [ ], function callback(result) {
console.log('Result from execute', result);
});
},
'Closing the browser': function (browser) {
browser.browserEnd();
},
};
However, when I run this I get the following error;
Error while running .executeScript() protocol action: An error occurred while executing user supplied JavaScript. – javascript error: Cannot read properties of null (reading ...
Result from execute {
status: -1,
state: '',
code: '',
value: {
message: "javascript error: Cannot read properties of null (reading 'getAttribute')",
error: [
' (Session info: chrome=95.0.4638.69)',
' (Driver info: chromedriver=95.0.4638.54 (d31a821ec901f68d0d34ccdbaea45b4c86ce543e-refs/branch-heads/4638@{#871}),platform=Mac OS X 11.6.1 x86_64)'
]
},
errorStatus: 17,
error: 'An error occurred while executing user supplied JavaScript. – javascript error: Cannot read properties of null (reading ...',
httpStatusCode: 200
}
I thought that this would get the value of '3' from the data-page-view-threshold attribute, but it returns a null.
Am I using the correct code to 'extract' this attribute, or is there something obvious that I'm doing wrong with this? Thanks