javascript - Nightmare.js 错误 : Search failed: Nothing responds to "goto"

标签 javascript loops for-loop nightmare

当我在 night.js 中包含 vanilla JS 时遇到错误。我想确保数组中的每封电子邮件都输入到系统中。 for 循环是理想的选择,但我不断遇到错误,例如:

Search failed: Nothing responds to "goto"

这是我的代码:

var jquery = require('jquery');
var Nightmare = require('nightmare');
var nightmare = Nightmare({
    show: true,
    dock: true
});

var siteName = "*********";
var username = "*********";
var password = "*********";

var outboundEmailArray = [
  {
    "from_name": "TestOutbound",
    "email_username": "array1",
    "email_domain": "salesforce.com",
    "email_domain": "salesforce.com",
    "reply_to": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a4d0c1d7d0ebd1d0c6cbd1cac0e4d7c5c8c1d7c2cbd6c7c18ac7cbc9" rel="noreferrer noopener nofollow">[email protected]</a>"
  },
  {
    "from_name": "Tester",
    "email_username": "array2.0",
    "email_domain": "salesforce.com",
    "email_domain": "salesforce.com",
    "reply_to": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a6d2c3d5d2e9d3d2c4c9d3c8c2e6d5c7cac3d5c0c9d4c5c388c5c9cb" rel="noreferrer noopener nofollow">[email protected]</a>"
  }
];
//
// var outboundEmailSetup = function(outboundEmail){
//     nightmare
//     .goto("https://" + siteName + ".desk.com/login/new")
//     .type("input[id='user_session_email']", username)
//     .type("input[id='user_session_password']", password)
//     .click("#user_session_submit").wait(2000)
//     .goto("https://" + siteName + ".desk.com/admin/settings/mail-servers")
//     .click("#a-add-modal").wait(2000)
//     .type("input[id='postmark_outbound_mailbox_fromname']", outboundEmail.from_name).wait(2000)
//     .type("input[id='email_username']", outboundEmail.email_username).wait(2000)
//     .click("#from_select_4999").wait(2000)
//     .type("input[id='postmark_outbound_mailbox_reply_to']", outboundEmail.reply_to).wait(2000)
//     .click("#postmark_commit").wait(2000)
//     .click(".a-modal-bottom .a-button").wait(2000)
//     .evaluate(function() {})
//     .end()
//     .then(function(result) {
//         console.log(result)
//     })
//     .catch(function(error) {
//         console.error('Search failed:', error);
//     });
//   }

var outboundEmailSetup = function(i){
  if(i < outboundEmailArray.length) {
    nightmare
    .goto("https://" + siteName + ".desk.com/login/new")
    .type("input[id='user_session_email']", username)
    .type("input[id='user_session_password']", password)
    .click("#user_session_submit").wait(2000)
    .goto("https://" + siteName + ".desk.com/admin/settings/mail-servers")
    .click("#a-add-modal").wait(2000)
    .type("input[id='postmark_outbound_mailbox_fromname']", outboundEmailArray[i].from_name).wait(2000)
    .type("input[id='email_username']", outboundEmailArray[i].email_username).wait(2000)
    .click("#from_select_4999").wait(2000)
    .type("input[id='postmark_outbound_mailbox_reply_to']", outboundEmailArray[i].reply_to).wait(2000)
    .click("#postmark_commit").wait(2000)
    .click(".a-modal-bottom .a-button").wait(2000)
    .evaluate(function() {})
    .end()
    .then(function(result) {
        console.log(result)
    })
    .catch(function(error) {
        console.error('Search failed:', error);
    });
    outboundEmailSetup(i+1);
  }
}

outboundEmailSetup(0);

理想情况下,它将循环遍历 outboundEmailArray,运行该函数将电子邮件输入系统,重复直到到达数组末尾。

最佳答案

关键是避免同时多次调用 then 方法

您会找到有关该概念的非常详细的解释 here .

基本上,您要做的就是确保每个连续调用都在前一个调用 then 方法内发生

当我们事先知道我们要处理多少步时,这真的很简单。例如,如果我们要连续两次调用,代码将如下所示:

nightmare.goto('http://example.com')
  .title()
  .then(function(title) {
    console.log(title);
    nightmare.goto('http://google.com')
      .title()
      .then(function(title) {
        console.log(title);
      });
  });

请注意 goto 到 google.com 是如何位于 then 回调内的。

由于您正在处理循环,因此您的代码会更复杂一些。

var urls = ['http://example1.com', 'http://example2.com', 'http://example3.com'];
urls.reduce(function(accumulator, url) {
  return accumulator.then(function(results) {
    return nightmare.goto(url)
      .wait('body')
      .title()
      .then(function(result){
        results.push(result);
        return results;
      });
  });
}, Promise.resolve([])).then(function(results){
    console.dir(results);
});

我认为源链接比我更好地解释了这段代码:-)

The above executes each Nightmare queue in series, adding the results to an array. The resulting accumulated array is resolved to the final .then() call where the results are printed.

关于javascript - Nightmare.js 错误 : Search failed: Nothing responds to "goto",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40250732/

相关文章:

javascript - PowerPoint JavaScript API 中是否有类似 OnSlideShowPageChange 的 API?

loops - 在 Ansible playbook 的 with_items 循环中注册变量

Java 8 forEach 带索引

javascript - 在 javascript 中动态填充对象数组时出现问题

javascript - 未触发提交类型输入的提交事件

当浏览器选项卡未聚焦时,Javascript setInterval/setTimeout 不起作用

javascript - 如何订购数组中的项目?

java - 这个 java for 循环条件有什么问题?

javascript - 如何在angularJs中使用数据列表

c - 程序拒绝进入for循环