【问题标题】:Chaining promisses with Webdriverio使用 Webdriverio 链接 Promise
【发布时间】:2016-07-20 16:39:38
【问题描述】:

首先,让我指出我是 node.js 和异步编程的新手,所以我的代码可能真的很糟糕。我正在尝试使用 webdriverio 和 Cheerio 构建一个 webscraper。在这个网络爬虫中,我必须进行查询,在内容页面和结果页面之间移动时抓取查询的结果,然后在结果用完后执行新的查询。这是我到目前为止提出的代码(假设客户端已经启动,并且正在从“.then()”操作调用函数“make_new_query()”):

function scrapt_content(){
// if array of content links is exhausted -> move to next page or perform new query
if(contents_pointer == contents.length){
    
    return client.isExisting("li.next-page > a").then(function(isExisting){
        // if there is a link to a a new page of results -> move to new page
        if(isExisting){
            return change_pages();
        } else {
            return make_new_query();
        };
    });

// change to new and scrapt it
} else {

    // var parsed = cheerio.load(res);
    ... scrap content using cherio ...
    .
    .
    .
    contents_pointer++;

    return scrapt_content(); 

    })
};
};

function change_pages(){

client
    .click("li.next-page > a")
    .getAttribute("h2 a", "href");
    .then(function(res){
        contents_pointer = 0;
        news_links = res;
        return scrapt_content();
    })
}

function make_new_query(){
.
.
.
client.url(new_query_url)
    .getAttribute("h2 > a", "href")
    .then(function(res){
        content_links = res;
        return scrapt_content();
    })
}
}

问题是,在到达要抓取的第一页内容后(代码执行查询并进入 content_links 数组中的第一个链接的页面),webdriver 关闭。就像代码首先执行函数change_pages,该函数调用了scrapt_content,然后提前终止。因此,我假设在此函数中链接操作时出现错误。在尝试链接这些操作时,谁能指出我的错误?

【问题讨论】:

    标签: javascript node.js webdriver-io


    【解决方案1】:

    您一定遗漏了一些代码,因为我不知道您在什么时候关闭了 webdriver。但是,您需要使用 promise 来确保函数在异步操作完成之前不会返回。由于您在节点中,因此您内置了大多数 ES6 功能,因此您可以在代码的最顶部添加“use strict”(以启用 ES6 功能),然后执行此操作(例如使用您的 scrapt_content 函数:

    //this function returns a promise
    function scrapt_content(){
      return new Promise(function(resolve, reject){
        InsertyourAsyncFunctionHere().then(function(){
          resolve();
        });
      });
    };
    
    //setting promise resolve/reject callbacks with then and catch
    scrapt_content.then(function(){
      //resolve (success) callback content here
    }).catch(function(err){
      //reject (error) callback contenthere
      console.log(err.message)
    });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-10
      相关资源
      最近更新 更多