Nightmare.js error: Search failed: Nothing responds to “goto”

空扰寡人 提交于 2019-11-28 14:30:39

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!