Quentin Del Quentin Del - 1 month ago 15
Javascript Question

Download a JSON behind a login page with nightmare

I am trying to download a heavy JSON which is behind a login page.
The document is about 5mb and takes approximatively 60 seconds to load on my connection. I tried this code but I keep having a empty object as a result.
Any ideas what is the issue?
Thanks!

var Nightmare = require('nightmare');
var nightmare = Nightmare({
typeInterval: 300,
show: true
});

nightmare
.goto('https://pageThatRequireToLoginThenDiplayJsonAsText.com')
.type('[name=email]', '')
.wait(1000)
.type('[name=email]', 'myemail')
.wait(1000)
.type('[name=password]', '')
.wait(1000)
.type('[name=password]', 'mypassword')
.click('[type=submit]')
.wait(25000)
.wait(25000)
.evaluate(function (page, done) {

document.documentElement
done()
})
.end()
.then(function (result) {
// fs.writeFileSync('testOutput.json', JSON.stringify(result));
console.log(JSON.stringify(result))
})
.catch(function (error) {
console.error('failed:', error);
});


Nightmare version: 2.8.1

Answer

Found the answer. Basically, I just had to load a second time the url.

var Nightmare = require('nightmare');
var nightmare = Nightmare({
  typeInterval: 300,
  show: true
});

nightmare
  .goto('https://pageThatRequireToLoginThenDiplayJsonAsText.com')
  .type('[name=email]', '')
  .wait(1000)
  .type('[name=email]', 'myemail')
  .wait(1000)
  .type('[name=password]', '')
  .wait(1000)
  .type('[name=password]', 'mypassword')
  .click('[type=submit]')
  .wait(25000)
  .goto('https://pageThatRequireToLoginThenDiplayJsonAsText.com')
  .wait(25000)
  .evaluate(function (page, done) {

    document.documentElement
    done()
  })
  .end()
  .then(function (result) {
    // fs.writeFileSync('testOutput.json', JSON.stringify(result));
    console.log(JSON.stringify(result))
  })
  .catch(function (error) {
    console.error('failed:', error);
  });