Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
Attribute of an element is outputting null

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;

enter image description here

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

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!