简体   繁体   English

等待噩梦js中的javascript函数

[英]Wait for javascript function in nightmare js

I am having trouble with the .wait() function of nightmare js. 我在噩梦js的.wait()函数中遇到麻烦。 I am trying to wait for a javascript function to return true and then proceed with the rest of the script. 我试图等待javascript函数返回true,然后继续执行脚本的其余部分。 Is there any way to do this because both ways I've tried don't work. 有没有办法做到这一点,因为我尝试过的两种方式都不起作用。 Thanks, all help is appreciated. 谢谢,感谢您的帮助。

var ifCompleted = function(){
     for(var i = 0; i < 101; i++){
          if(i == 100){
               return true;
          }
     }

}

nightmare
     .goto('http://www.google.com')
     .wait(function () {
          return typeof ifCompleted() !== undefined;
     })
     .goto('http://www.yahoo.com/')
     .catch(function (error) {
          console.error('Search failed:', error);
     });

The next wait function does not work either. 下一个等待功能也不起作用。

.wait(function () {
    return (ifCompleted() === true);
})

This does not work either. 这也不起作用。

.wait(function () {
    return (ifCompleted() != null);
})

This code is hypothetical, .wait(100) is not what I am trying to do 这段代码是假设的,.wait(100)不是我要尝试执行的操作

I think you want to use then(). 我认为您想使用then()。

const Nightmare = require('nightmare');
var nightmare = Nightmare({
  show: true,

});

nightmare
  .goto('http://www.google.com')
  .wait(1000)
  .evaluate(function() {
    return "Return after 1 second";
  })
  .then(function(result) {
    console.log(result);
    nightmare.goto('http://www.yahoo.com/')
      .wait(1000)
      .then(function() {
        console.log("Welcome to yahoo.")
      }).catch(function(err) {
        console.log(err);
      });
  }).catch(function(err) {
      console.log(err);
  });

You might want to check Check https://github.com/rosshinkley/nightmare-examples/blob/master/docs/common-pitfalls/async-operations-loops.md 您可能需要检查Check https://github.com/rosshinkley/nightmare-examples/blob/master/docs/common-pitfalls/async-operations-loops.md

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM