Javascript-Nightmare.JS无限滚动操作
Javascript - Nightmare.JS infinite scroll action
我做错了什么?我想向下滚动一页,直到选择器消失。
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)
})
我正在使用:
.goto("link")
.scrollPage()
(移植噩梦#625的原始答案。)
这是一个非常天真的方法来回答您的问题:
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');
});
这种方法有问题:当你面对的页面实际上是无限滚动时,上面的内容永远不会结束。此外,可以用等待滚动元素计数改变来代替.wait()
调用,以可能减少延迟并提高鲁棒性。不过,这应该足以让你开始。
编辑:您询问了选择器的问题,您可以将while
子句替换为使用选择器,而不是考虑增加高度。从臀部看,类似于:
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');
});
这种方法仍然存在问题:首先,您依赖页面来满足while
查询,这不一定能保证。
相关文章:
- 支持在画布上滚动和拖动操作
- Javascript-Nightmare.JS无限滚动操作
- 使用jQuery可以禁用除滚动条之外的所有单击操作
- 防止jQuery操作在滚动时再次发生
- 滚动不'在移动浏览器中执行手势操作后无法工作
- 当您滚动到特定页面位置时,如何执行此操作,该功能将启动一次
- 对多个滚动事件执行一次操作并防止滚动
- 使用 jQuery 滚动后的操作
- 单击提交按钮后,屏幕向下滚动,不执行任何操作
- 在 html/js 中没有点击功能的滚动页面操作
- 何时以及如何调用查找页面滚动操作的 javascript 函数
- 如何在滚动手风琴分区上进行操作
- 反向鼠标滚轮用户操作.向下滚动=在jquery中向右滚动
- Dojo DataGrid虚拟滚动操作方法
- 浏览器滚动事件是否可能由DOM操作(而不是用户操作或显式调用)触发?.ScrollTop美元)
- 向下滚动到底部.然后重新加载页面并再次执行此操作
- 对滚动事件执行一些操作
- 滚动速度操作css jquery
- 对窗口滚动的每n个像素执行一些操作
- 强制垂直滚动用户操作显示为水平滚动