如何在CasperJS中设置wait()的值



这是我的完整代码。。我想要的是casper.wait有1-3秒的随机等待时间。如果我让"casper.wait(1000,function(){"输入一个数值,如果它有效,但是casper.wwait(time,function(

casper.then(function() {
  this.echo('Looking random number.....');
  rrandom = Math.round(Math.random() * 3);
  if (rrandom == 1) {
    time = 1000
  }
  if (rrandom == 2) {
    time = 2000
  }
  if (rrandom == 3) {
    time = 3000
  }
});
casper.wait(time, function() {
  this.echo('result');
});
casper.run();

在您的样本中random有时将等于0,因为Math.round()舍入<0.49至零。因此time有时会被未定义,从而破坏脚本。

我建议这样做:

var time;
casper.then(function() {
  var maxSecTimeout = 3;
  this.echo('Pausing for ' + maxSecTimeout + ' seconds');
  time = Math.ceil(Math.random() * maxSecTimeout) * 1000;  
});
casper.wait(time, function() {
  this.echo('result');
});
casper.run();

最新更新