Javascript - Nightmare.JS infinite scroll action

谁说胖子不能爱 提交于 2019-12-11 12:58:08

问题


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 () {
            var hitRockBottom = false; 
            while (!hitRockBottom) {
                // console.log("1");
            // Scroll the page (not sure if this is the best way to do so...)
            this.scrollTo(100000, 0);

            // Check if we've hit the bottom
            hitRockBottom = this.evaluate(function() {
                // console.log("0");
                return this.exists('selector') === null;
            }); }
          }, done)
        })

I'm using:

.goto("link")
.scrollPage()

回答1:


(Porting my original answer from Nightmare #625.)

This is a very naive method to answer your question:

var Nightmare = 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() {
      return document.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:

var Nightmare = 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() {
      return document.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.



来源:https://stackoverflow.com/questions/37090854/javascript-nightmare-js-infinite-scroll-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!