gpt4 book ai didi

javascript - NightmareJS 的异步问题

转载 作者:太空宇宙 更新时间:2023-11-04 00:35:22 24 4
gpt4 key购买 nike

我正在尝试使用 Nightmarejs 构建一个脚本,该脚本能够一遍又一遍地单击一个按钮,就像评论部分底部的 youtube 按钮一样,每次按下它时都会加载较旧的评论(例如:youtube .com/watch?v=pZISJqJbQuU&list=RDpZISJqJbQuU#t=3) 并在没有更多按钮可点击时停止。

我尝试过调用evaluate,只是为了先调用end()方法并取消进程。我尝试过使用 setTimeout 、 setInterval 、then() 将循环转换为递归。每次,evaluate() 都会完成它的工作,但不会退出(只是挂起),或者由于与 end() 的竞争条件而在完成它的工作之前退出。

有经验丰富的 nightjs 用户吗?

    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() 函数不能异步,至少在 #819 之前不能。 (或类似的东西)包含在内。换句话说,使用 setTimeout() 进行评估是行不通的。此外,我认为在这种情况下这并不是一个特别好的方法,即使它可以开箱即用。

顺便说一句,您可能想要给予 this阅读nightmare-examples中的示例。它简要介绍了链接多个操作和循环。

您可能可以使用递归函数,但必须小心强制执行停止条件。 (您可能还想看看 #625 , Action 是滚动的,但问题是相似的。)从臀部来看,我认为这样的东西可能会让您接近:

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

关于javascript - NightmareJS 的异步问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39275971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com