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

How to set a value for wait() in CasperJS?

这是我的完整代码..我想要的是 casper.wait 有 1-3 秒的随机等待时间。如果我把“casper.wait(1000,function(){”输入一个数值,如果有效,但是casper.wait(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();

在您的示例中 rrandom 有时会等于 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();