gpt4 book ai didi

javascript - 如何在casperjs中以特定间隔运行循环?

转载 作者:行者123 更新时间:2023-12-02 18:02:46 26 4
gpt4 key购买 nike

我想使用 casperjs 自动点击 id='vote' 按钮 3 次,间隔时间为 5 秒,为此我编写了下面的代码

 var casper = require('casper').create();

casper.start('http://www.mysite.com/mypage');

casper.repeat(3, function() {
this.click('#vote');
});

casper.then(function() {
console.log('clicked vote ,and voted successfully , and curernt url is ' + this.getCurrentUrl());
});
casper.run();

但这只能工作一次,我想做的是以特定的时间间隔重复循环 3 次,因为这是一个关于单击投票按钮的 JavaScript ajax 帖子,它会更新数据库,它是否会执行所有 3 次单击立刻?我必须做什么才能避免这种情况,并更新数据库 3 次?

最佳答案

这是使用 wait 的解决方案和 each 。下面单击一个按钮,等待 3 秒,然后循环遍历数组的每个值。循环有效,我无法测试您的点击。

var casper = require('casper').create ({
waitTimeout: 15000,
stepTimeout: 10000,
verbose: true,
viewportSize: {
width: 1400,
height: 768
},
pageSettings: {
"userAgent": 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.10 (KHTML, like Gecko) Chrome/23.0.1262.0 Safari/537.10',
"loadImages": false,
"webSecurityEnabled": false,
"ignoreSslErrors": true
},
onWaitTimeout: function() {
//throw new Error
},
onStepTimeout: function() {
//throw new Error
}
});

casper.on('remote.message', function(msg) {
console.log('***REMOTE MESSAGE***:', msg);
//throw new Error
});

// array of how many clicks
var clicks = [];
clicks[0] = 'click1';
clicks[1] = 'click2';
clicks[2] = 'click3';

casper.start('http://www.google.com/'); //+++ change the url

// function to click button
casper.Clicker = function () {
this.click('#vote');
this.echo('I just clicked');
return true;
};

//function to wait set time
casper.Waiter = function () {
// adjust wait time between clicks
this.wait(3000, function() {
this.echo('I waited for 3 seconds.');
});
return true;
};

// loop
casper.then(function() {
this.each(clicks, function() {
this.waitFor(function check() {
return this.Clicker();
});
this.waitFor(function check() {
return this.Waiter();
}, function then() {
this.echo('Olayy!');
});
});
});

casper.run();

如果您想要一个带有设置点击次数(不使用数组)和设置间隔的循环:

var casper = require('casper').create ({
waitTimeout: 15000,
stepTimeout: 10000,
verbose: true,
viewportSize: {
width: 1400,
height: 768
},
pageSettings: {
"userAgent": 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.10 (KHTML, like Gecko) Chrome/23.0.1262.0 Safari/537.10',
"loadImages": false,
"webSecurityEnabled": false,
"ignoreSslErrors": true
},
onWaitTimeout: function() {
//throw new Error
},
onStepTimeout: function() {
//throw new Error
}
});

casper.on('remote.message', function(msg) {
console.log('***REMOTE MESSAGE***:', msg);
//throw new Error
});

// how many clicks
var clickCount = 3

casper.start('http://www.google.com'); //+++ change the url

// function to click button
casper.Clicker = function () {
this.click('#vote');
this.echo('I just clicked');
return true;
};

//function to wait set time
casper.Waiter = function () {
// adjust wait time between clicks
this.wait(3000, function() {
this.echo('I waited for 3 seconds.');
});
return true;
};

// loop
casper.then(function() {
for (var i = 0; i < clickCount; i++) {
this.waitFor(function check() {
return this.Clicker();
});
this.waitFor(function check() {
return this.Waiter();
});
}
this.echo('Olayy!');
});

casper.run();

关于javascript - 如何在casperjs中以特定间隔运行循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20375665/

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