【问题标题】:call a function if nightmare waitTimeout exceeded如果噩梦等待超时,则调用函数
【发布时间】:2017-05-16 18:23:18
【问题描述】:

使用 nightmareJS,如果超过 waitTimeout 限制,有什么方法可以调用函数?现在它正在给我一个承诺拒绝警告

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: .wait() timed out after 10000msec

我想知道如果收到此错误,是否有任何方法可以调用函数。我的代码结构如下:

var Nightmare = require('nightmare');       
var nightmare = Nightmare({ show: true, waitTimeout: 10000});
nightmare.goto(my_url);
nightmare.wait(my_element);
nightmare.evaluate(function() {...});
nightmare.then(function(result) {...code that could give a promise warning...});

我可以在 goto 和 then 语句中添加一个 catch 子句(正如 Belfordz 建议的那样),这会起作用,但是 then 语句中还有其他嵌套的 then 语句。我更喜欢一个可以捕获任何 Promise 警告或抛出的超时错误的语句。

【问题讨论】:

  • 听起来你只需要在承诺拒绝的内容中添加一个 catch 子句

标签: javascript web-scraping phantomjs nightmare


【解决方案1】:

该错误表示在10秒的延迟内没有在当前页面上找到my_element

但是,按照您的编码方式,您正在同步调用方法gotowaitevaluatethen,而它们应该由内部承诺机制调用,即通过“链接”调用,否则它们很快就会被执行:

var Nightmare = require('nightmare');       
var nightmare = Nightmare({ show: true, waitTimeout: 10000});
nightmare.goto(my_url) // <-- don't end the statement here, continue the chain:
    .wait(my_element)
    .evaluate(function() {...})
    .then(function(result) {...});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 2010-12-21
    • 2020-03-14
    • 2010-11-13
    相关资源
    最近更新 更多