2017-03-04 49 views
1

我正在学习梦魇。试图访问一个网站,登录,然后反复点击一个按钮,以使更多的数据出现,直到该按钮不再存在。我为了插图做了一个虚拟账户。恶梦循环点击事件

我已成功登录并在第一次单击该按钮,但是当我尝试再次单击它时,它会记录一个错误,它找不到'.more-checkins'元素。

最终,我希望这段代码生活在一个循环中,而不是告诉代码点击......等等,然后再次单击。帮助设计,也将不胜感激。

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)) 

回答

0

两部分答案:一,继续nightmare-examples阅读asynchronous operations and loops。这将有助于您获得有关承诺迭代的背景。

二,在梦魇回购中存在类似的问题 - #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'); 
}); 

希望这给你足够的上手。