Nightmare.js错误:搜索失败:没有响应“goto” [英] Nightmare.js error: Search failed: Nothing responds to "goto"

查看:187
本文介绍了Nightmare.js错误:搜索失败:没有响应“goto”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

$ p $ 搜索失败:没有响应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: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)。 (2000)
// .evaluate(function(){})
// .end(。a-modal-bottom .a-button ()
// .then(function(result){
// console.log(result)
//})
// .catch(function(error){
// console.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 / (2000)
.type(input [id ='postmark_outbound_mailbox_fromname'],outboundEmailArray [i] .fromit).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循环,运行函数将电子邮件输入到系统中,重复直到它到达数组的末尾。

你会发现关于这个概念的非常详细的解释 here。



基本上你要做的是确保每个连续的调用发生在前面的调用然后方法



事实上,事前知道我们有多少步处理。例如,如果我们想连续两次调用,代码将如下所示:

  nightmare.goto('http: ()函数(title){
console.log(title);
nightmare.goto('http:/ /example.com')
.title()
.then ($ title)
.title()
.then ;

注意 goto to google.com在内,然后是回调。



由于你正在处理一个循环,所以你的代码会多一点复杂。

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

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


上面执行每个Nightmare队列,将结果
添加到数组中。得到的累加数组被解析为最后的
.then()调用,其结果被打印。



I am running into an error, when including vanilla JS in nightmare.js. I want to make sure that every email in my array is inputted into the system. A for loop would be ideal, but I have continually run into error such as:

Search failed: Nothing responds to "goto"

Here is my code:

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

Ideally, it would loop through the outboundEmailArray, run the function to input the emails into the system, repeat until it has reached the end of the array.

解决方案

The key is to avoid multiple calls to the then method concurrently

You will find a very detailed explanation about that concept here.

Basically what you have to do is make sure each consecutive call take place within the previous call then method

This is really straightforward when we know beforehand how many steps we are dealing with. For example, if we want to make two consecutive calls, the code would be like this:

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

Notice how the goto to google.com is inside the then callback.

Since you're dealing with a loop, your code would be a little more sophisticated.

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

I think the source link explains this code better than I can :-)

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.

这篇关于Nightmare.js错误:搜索失败:没有响应“goto”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆