与NightmareJS的asynchronous问题

我正在尝试使用nightmarejs来构build一个脚本,这个脚本可以一次又一次地点击一个button,就像在评论部分底部的那个button一样,每次按下它时都会载入较旧的评论(例如:youtube.com/观看?v = pZISJqJbQuU&list = RDpZISJqJbQuU#t = 3),并在没有更多button被点击时停止。

我曾尝试调用评估,只为end()方法先被调用,并取消进程。 我已经尝试使用setTimeout,setInterval,然后(),将循环转换为recursion。 每次评估()都会完成它的工作,但是在完成工作之前不能退出(只是挂起)或者退出,因为和end()的竞争条件。

有没有经验丰富的nightmarejs用户?

function youtube_button_clicker(group) { var nightmare = Nightmare({ show: true }); nightmare .goto(url) .inject('js', 'jquery-3.1.0.js') .then( () => nightmare. evaluate(function() { for (i=0;i>LEN;i++) { setTimeout(() => { $(button_element).click() }, i * 2000); } return "done" })) .catch(function (error) { console.error('Search failed:', error); }); } 

删除.end()方法并挂起,重新放置.end(),并跳过该过程 – 尽早退出。 我能做什么 ?

.evaluate()函数不能是asynchronous的,至less不包括#819 (或类似的东西)。 换句话说,用setTimeout()评估是行不通的。 另外,我认为在这种情况下这不是一个特别好的途径,即使它是可能的。

nightmare-examples一句,你可能想要在nightmare-examples来读一下这个例子。 它简要介绍了链接多个操作和循环。

你可能会使用recursion函数,但是你必须小心执行一个停止条件。 (你也许还想看看#625 ,这个动作是滚动的,但问题是相似的。)从髋关节,我想这样的事情可能会让你接近:

 var iterations = 0; var reclick = function(nm) { return nm .click('#some-button') .wait('#element-you-check-for') .then(function() { // you could also use `.visible()` or something before this `.then()` // depends on your use case // here, I'm using the number of iterations if (iterations++ < 10) { return reclick(nm); } return; }); } nightmare .goto('http://my-domain.tld') .then(function() { return reclick(nightmare); }) .then(function(){ // ... other actions })