如何在 CasperJS onWaitTimeout 事件处理程序中回显缺少的选择器?

How to echo the missing selector in a CasperJS onWaitTimeout event handler?

我的脚本中有很多 casper.waitForSelector 部分。无论如何,我想知道它何时超时。因此我补充说:

casper.options.onWaitTimeout = function() {
    //how to echo Selector which timed out here
};

此外,我想回显超时的选择器。

onWaitTimeout的签名是:

onWaitTimeout(Integer timeout, Object details)

遗憾的是没有正确记录。传递给函数的 details 对象包含代表选择器的 selector 属性。如果它是一个 XPath 选择器,那么您将需要获取它的 path 属性。

casper.options.onWaitTimeout = function(timeout, details) {
    var selector = details.selector.type === 'xpath' ? 
            details.selector.path : details.selector;
    this.echo("Wait timed out after " + timeout + " msec with selector: " + selector);
};

请注意,此函数将捕获所有等待超时,如果您愿意,您将不得不自己退出脚本。

查看源代码总是一个好主意:1 & 2