CasperJS没有超时

时间:2014-09-01 10:02:57

标签: javascript phantomjs casperjs

我有一个CasperJS脚本,它可以执行各种功能,并记录每个步骤后发出信号所需的时间。这个过程有效。我遇到过不经常的实例,我没有调用我指定的自定义超时。我知道这是因为步进时间大于超时。我的CasperJS设置如下所示:

var session1 = require('casper').create({
    logLevel: 'debug',
    waitTimeout: 60000,
    userAgent: 'Mozilla/5.0'
});

该功能如下所示:

session1.waitForSelector('#Selector', function () {
    this.emit('logged.in');
    this.clickLabel('Clients', 'a');
}, function timeout() {
    this.emit('genericTimeout', 'Could not log in');
});

在某些情况下,达到了超时功能,有时甚至很少,但事实并非如此。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

@ Ka0s,显然你的代码一切正常。只是事后的想法和你的代码的建议...我注意到你正在使用waitForSelector,我更喜欢使用waitFor因此他可以处理我职责范围内的任何其他变量。

如果你注意的话,另一点很酷的是,你可以设置超时的默认时间,此外,在每次使用中,你可以强制执行不同的超时,我非常感谢你的工作!

以下是一个例子:

// Default setting:
casper.options.timeout = 30000; // 30s for loading a page
casper.options.stepTimeout = 60000; // = 60s 1m to perform the processing ofeach step

之后,在每个代码块中,如果需要,可以强制执行不同的超时...

casper.waitFor (check function () {
    return this.evaluate (function () {return (__utils getElementByXPath __ ('XPATH') = null);.!});
} Then function () {
    // Then
}, Function timeout () {
    // Timeout
} 10000); // Forcing timeout 10 seconds waiting for the element on the page

在waitForSelector中以waitFor的方式工作方式相同,请查看此处的文档waitFor()和此处waitForSelector()

相关问题