如何使casperjs继续而不是在出现异常时退出
How to make casperjs continue instead of exit on exception?
当我使用CasperJS处理web时,我发现了一个奇怪的情况。即使我使用try-catch块,程序也会在出现异常时退出!我的代码如下。我希望程序可以继续运行下一个循环。但是,当找不到iframe
时,会抛出类似CasperError: Frame number "1" is out of bounds.
的异常,整个程序就会退出。catch块中的myStore
函数也没有运行。
有人能帮我们吗?
try {
// find the iframe and then fill in the message
casper.withFrame(1, function() {
casper.evaluate(function(message) {
// some function code
});
casper.wait(1000, function() {
myStore(stores, index+1);
// when the iframe not found, function myStroe will not be run
});
});
} catch (err) {
output(false, "error=" + err.message);
myStore(stores, index+1); // myStroe will not be run on Exception
}
我尝试了Artjom B给出的例子,但它不起作用。
var frameExists = false;
casper.withFrame(1, function() {
frameExists = true;
casper.evaluate(function(message) {
// some function code
});
});
casper.wait(3000, function() {
if (frameExists) {
// the program run this branch and got stuck in the nonexistent selector since the frame is not found.
casper.click("input#send");
// some function code
output(true, "index=" + index + ";storeId=" + store.id + ";succeeded");
myStore(stores, index+1);
} else {
output(false, "index=" + index + ";storeId=" + store.id + ";error=" + err.message);
myStore(stores, index+1);
}
});
这很奇怪。我不知道为什么程序会遇到frameExists === true
错误的分支。
有一个方便的小选项,叫做exitOnError
:
一开始:
var casper = require('casper').create({
exitOnError: false
});
或更高版本:
casper.options.exitOnError = false;
如果你想让myStore()
在不考虑帧存在的情况下运行,你可以这样做:
casper.then(function(){
var frameExists = false;
// find the iframe and then fill in the message
casper.withFrame(1, function() {
frameExists = true;
casper.evaluate(function(message) {
// some function code
});
casper.wait(1000, function() {
myStore(stores, index+1);
// when the iframe not found, function myStroe will not be run
});
});
casper.then(function(){
if (!frameExists) {
output(false, "error=" + err.message);
myStore(stores, index+1); // myStroe will not be run on Exception
}
});
});
相关文章:
- 弹出窗口出现,然后退出
- 退出全屏不起作用
- 基于两个条件退出While循环
- 如何准确执行加载脚本&退出弹出窗口
- 在javaservlet doPost方法中启动线程时,无法返回异常消息
- document.applet.method在Mounatin Lion上抛出safari 6+JDK7异常
- firefox插件:退出不工作的应用程序观察器
- 为什么浏览器没有为语法错误抛出异常
- java,javascript签名的小程序(文件选择,ftp上传)安全性/套接字异常
- node.js和express中的异常处理
- 派生进程的stdout在管道传输时工作异常
- NodeJS和pg promise,捕获PostgreSQL异常
- 当显式定义控制器参数时,默认模型绑定器会发生异常
- 显示引导弹出操作异常的逻辑
- 调用谷歌地图路线服务时未捕获的异常
- web配置http到https重定向异常
- 转换异常时间&Datestring到JavaScript日期
- 为什么在python中使用异常比javascript更容易接受
- PHP REST服务抛出异常——希望避免控制台错误
- 如何使casperjs继续而不是在出现异常时退出