向下滚动页面事件x秒 [英] Scroll down page event for x seconds

查看:66
本文介绍了向下滚动页面事件x秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前能够在CasperJS的帮助下向下滚动页面。我看到了文章,该文章介绍了如何根据某些属性。但是,我不想基于某个元素的可见性,但想知道是否有一种方法可以设置计时器,使其在退出之前应该向下滚动多长时间。怎么会这样的事情?

I am currently able to scroll down a page with the help of CasperJS. I saw this article on how to scroll infinite pages based on the visibility of certain attributes. However I don't want to base it on the visibility of an element but was wondering if there is a way to set a timer on how long it should stay scrolling down before exiting out. How would be able to such thing?

//function to scroll
function tryAndScroll(casper) {
    casper.page.scrollPosition = {
        top: casper.page.scrollPosition["top"] + 40000,
        left: 0
    };
}

/**
 * Everything starts here!
 * I use the mobile version of facebook as the DOM is waaay simpler to scrape.
 */
casper.start('https://www.somesite.com', function() {
});

casper.then(function() {
    tryAndScroll(this);
});

casper.then(function() {;
    this.exit();
});

casper.run();


推荐答案

这是一种简单的方法:

function tryAndScroll(casper) {
    casper.page.scrollPosition = {
        top: casper.page.scrollPosition["top"] + 300,
        left: 0
    };
}

casper.start(url).then(function() {
    var self = this;
    var intervalId = setInterval(function(){
        tryAndScroll(self);
    }, 100); // retry interval
    self.wait(10000 /* infinite scroll timeout */, function(){
        clearInterval(intervalId);
    });
}).run();

因为 setInterval()不是CasperJS阶跃函数,这实际上超出了CasperJS的控制流程。 wait()是必需的,以便CasperJS在滚动期间不会执行其他操作。

Since setInterval() is not a CasperJS step function, this essentially break out of the control flow of CasperJS. The wait() is necessary so that CasperJS doesn't execute something else during the scroll.

另外,您不能使用40000像素的滚动距离。这太大了,PhantomJS将无法截屏。

Also, you can't use a scroll distance of 40000 pixels. This is too big and PhantomJS won't be able to take a screenshot.

这篇关于向下滚动页面事件x秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆