javascript - 等待 Nightmare js中的javascript函数

标签 javascript node.js nightmare

我在 Nightmare js的.wait()函数中遇到麻烦。我试图等待javascript函数返回true,然后继续执行脚本的其余部分。有没有办法做到这一点,因为我尝试过的两种方式都不起作用。谢谢,感谢您的帮助。

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);
     });

下一个等待功能也不起作用。
.wait(function () {
    return (ifCompleted() === true);
})

这也不起作用。
.wait(function () {
    return (ifCompleted() != null);
})

这段代码是假设的,.wait(100)不是我要尝试执行的操作

最佳答案

我认为您想使用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);
  });

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

关于javascript - 等待 Nightmare js中的javascript函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44489671/

相关文章:

javascript - 如何在 iOS 中为 HTML、CSS 和 Javascript 页面制作包装器

JavaScript 减去时差

javascript - 从 html 表单获取 javascript 变量

node.js - 了解 redis-store pub-sub 中的 "dispatch" channel

node.js - 错误: spawn ENOTDIR html-pdf in mac

javascript - Node.js Express.js secret 静态文件

javascript - 文本在 div 之外流动,溢出 + 定位 div 在父 div 的底部(有溢出)

javascript - 使用 Electron (NightareJS) 重复单击页面上的元素

javascript - Firefox 点击和 chrome 点击有什么区别?

javascript - 在 NightmareJS 实例中保留 cookie