Nightmare.js 错误:搜索失败:没有任何内容响应“goto”

Eri*_*ark 2 javascript loops for-loop nightmare

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

Search failed: Nothing responds to "goto"
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

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": "testOutbound@salesforce.com"
  },
  {
    "from_name": "Tester",
    "email_username": "array2.0",
    "email_domain": "salesforce.com",
    "email_domain": "salesforce.com",
    "reply_to": "testOutbound@salesforce.com"
  }
];
//
// 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);
Run Code Online (Sandbox Code Playgroud)

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

fel*_*gdr 5

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

您将在这里找到有关该概念的非常详细的解释。

基本上你要做的就是确保每个连续的调用都发生在前一个调用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);
      });
  });
Run Code Online (Sandbox Code Playgroud)

goto请注意to 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);
});
Run Code Online (Sandbox Code Playgroud)

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

上面的代码串行执行每个 Nightmare 队列,并将结果添加到一个数组中。生成的累积数组将解析为最终的 .then() 调用,并在其中打印结果。