Javascript 通过点击事件的恶梦循环
我在学噩梦。尝试访问网站、登录,然后反复单击按钮以显示更多数据,直到按钮不再存在。我做了一个虚拟的说明 我已成功登录并在第一次单击按钮,但当我尝试再次单击按钮时,它会记录一个错误,无法找到“.more checkins”元素 最终,我希望这段代码活在一个循环中,而不是告诉代码单击…等待…然后再次单击。帮助设计,也将不胜感激Javascript 通过点击事件的恶梦循环,javascript,web-scraping,phantomjs,nightmare,Javascript,Web Scraping,Phantomjs,Nightmare,我在学噩梦。尝试访问网站、登录,然后反复单击按钮以显示更多数据,直到按钮不再存在。我做了一个虚拟的说明 我已成功登录并在第一次单击按钮,但当我尝试再次单击按钮时,它会记录一个错误,无法找到“.more checkins”元素 最终,我希望这段代码活在一个循环中,而不是告诉代码单击…等待…然后再次单击。帮助设计,也将不胜感激 const Nightmare = require('nightmare') const untappdURL = 'https://untappd.com/user/bee
const Nightmare = require('nightmare')
const untappdURL = 'https://untappd.com/user/beerFan2017'
Nightmare({
show: true,
openDevTools: true,
waitTimeout: 90000000 // increase the default timeout to test things
})
.goto(untappdURL)
.click('.more_checkins')
.type('#username', 'beerFan2017')
.type('#password', 'Testing2017')
.click('input[type="submit"]')
.wait('.stats')
.click('.more_checkins')
.evaluate(function() {
return console.log('Begin waiting');
})
.wait(5000)
.evaluate(function() {
return console.log('Waiting end');
})
.click('more_checkins')
.then(result => console.log(result))
.catch(error => console.error(error))
两部分回答:第一,继续阅读噩梦示例。这将帮助您获得关于承诺迭代的一些背景知识 第二,噩梦回购协议中也存在类似的问题——当你到达滚动条的末端时,继续加载更多内容。示例实现(非常幼稚,请小心): 希望这能给你足够的时间开始
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');
});