Skip to content Skip to sidebar Skip to footer

Javascript - Nightmare.js Infinite Scroll Action

What am I doing wrong here? I want to scroll-down a page until the selector is gone. Nightmare.action('scrollPage', function (done) { this.evaluate_now(function () {

Solution 1:

(Porting my original answer from Nightmare #625.)

This is a very naive method to answer your question:

varNightmare = require('nightmare');
var vo = require('vo');
var nightmare = Nightmare({
  show: true
});

var run = function * () {
  yield nightmare.goto('http://someInfiniteScrollPage.tld');

  var previousHeight, currentHeight=0;
  while(previousHeight !== currentHeight) {
    previousHeight = currentHeight;
    var currentHeight = yield nightmare.evaluate(function() {
      returndocument.body.scrollHeight;
    });
    yield nightmare.scrollTo(currentHeight, 0)
      .wait(3000);
  }
  yield nightmare.end();
};

vo(run)(function(err) {
  console.dir(err);
  console.log('done');
});

This approach has problems: when you're going against a page that actually is an infinite scroll, the above will never end. Also, the .wait() call could be replaced with waiting for the scroll element count to change to possibly reduce latency and increase robustness. Still, this should be enough to get you started.


EDIT: You asked about a selector, you could swap the while clause to use a selector instead of looking at increasing height. From the hip, something like:

varNightmare = require('nightmare');
var vo = require('vo');
var nightmare = Nightmare({
  show: true
});

var run = function * () {
  yield nightmare.goto('http://someInfiniteScrollPage.tld');

  while(document.querySelectorAll('.someClass').length > 0) {
    var currentHeight = yield nightmare.evaluate(function() {
      returndocument.body.scrollHeight;
    });
    yield nightmare.scrollTo(currentHeight, 0)
      .wait(3000);
  }
  yield nightmare.end();
};

vo(run)(function(err) {
  console.dir(err);
  console.log('done');
});

This approach still has problems: for one, you're relying on the page to satisfy the while query, which isn't necessarily guaranteed.

Post a Comment for "Javascript - Nightmare.js Infinite Scroll Action"