[英]Nightmarejs .click() on each element with delay between
I am trying to make simple follow script with Nightmarejs. 我试图用Nightmarejs制作简单的脚本。 It should work in next way:
它应该以下一种方式工作:
What I have so far is this, and it works with no errors, but it clicks only on first follow button and that is end: 到目前为止我所拥有的是它,它没有任何错误,但只点击第一个按钮,结束:
var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true })
nightmare
.goto('http://example.com/')
.click('.buttonOpenModal')
.wait(4000)
.click('.buttonFollow')
.end()
.then(function (result) {
console.log(result)
})
.catch(function (error) {
console.error('Search failed:', error);
});
I tried to loop clicking on follow buttons like this but it gives me error $ is not defined 我试图循环点击这样的跟随按钮,但它给我错误$未定义
var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true })
nightmare
.goto('http://example.com/')
.click('.buttonOpenModal')
.wait(4000)
.evaluate(function(){
$('.buttonFollow').each(function() {
$(this).click();
});
})
.end()
.then(function (result) {
console.log(result)
})
.catch(function (error) {
console.error('Search failed:', error);
});
I believe that for someone who is experienced in Nightmarejs this will be simple task, but I just started with it and been struggling with it for 2 days now. 我相信对于那些在Nightmarejs经验丰富的人来说,这将是一项简单的任务,但我刚刚开始使用它并且现在已经苦苦挣扎了2天。
I would really appreciate any help. 我真的很感激任何帮助。
You get $ is not defined
because this syntax is part of jQuery, when you write NightmareJS script it uses pure javascript. 你得到
$ is not defined
因为这个语法是jQuery的一部分,当你编写NightmareJS脚本时它使用纯javascript。
You can load jquery library (before evaluateing) thanks to function .inject("js", "https://code.jquery.com/jquery-3.1.0.min.js")
由于函数
.inject("js", "https://code.jquery.com/jquery-3.1.0.min.js")
您可以加载jquery库(在评估之前.inject("js", "https://code.jquery.com/jquery-3.1.0.min.js")
We need a synchronous sleep, else NightmareJS may end the function ̀ evaluate
before it finishes. 我们需要一个同步的睡眠,否则NightmareJS可以结束这个函数
evaluate
完成之前。 (I found the code for sleep
on this thread: https://stackoverflow.com/a/17532524/6479780 ) If you want to try asynchronous sleep, you have to use setTimeout(callback, millis)
Here is what I would do, using pure javascript : (我在这个帖子上找到了
sleep
代码: https : //stackoverflow.com/a/17532524/6479780 )如果你想尝试异步睡眠,你必须使用setTimeout(callback, millis)
这是我要做的,使用纯JavaScript:
var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true })
nightmare
.goto('http://example.com/')
.click('.buttonOpenModal')
.wait(4000)
.evaluate(function(){
function sleep(ms) {
var start = new Date().getTime(), expire = start + ms;
while (new Date().getTime() < expire) { }
return;
}
var list = document.querySelectorAll('.buttonFollow');
list.forEach(function(elt){
elt.click();
//We need synchronous sleep here
sleep(2000); //Wait 2 seconds
});
})
.end()
.then(function (result) {
console.log(result)
})
.catch(function (error) {
console.error('Search failed:', error);
});
Try this version: 试试这个版本:
var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true });
nightmare
.goto('http://example.com/')
.click('.buttonOpenModal')
.wait('div.Buttons')
.evaluate(function () {
var interval = setInterval(function () {
var btn = document.querySelectorAll('div.Buttons');
if (undefined == btn || 0 == btn.length) {
clearInterval(interval);
}
else {
btn[0].click();
}
}, 5000);
})
.end()
.then(function (result) {
console.log(result)
})
.catch(function (error) {
console.error('Search failed:', error);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.