请考虑以下代码:
var steps = [];
var testindex = 0;
var testError = 0;
var loadInProgress = false;
var webPage = require('webpage');
var page = webPage.create();
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36';
page.settings.javascriptEnabled = true;
page.settings.loadImages = false;
phantom.cookiesEnabled = true;
phantom.javascriptEnabled = true;
steps = [
//Step 1 - Open site
function() {
console.log('Step 1 - Open UK site');
page.open("http://www.example.com/", function(status) {
if (status !== 'success') {
console.log("Adidas UK is loading for long");
}
});
},
function() {
console.log('Step 2 - Search for the product');
page.evaluate(function(login, password) {
bla bla bla ...
});
},
function() {
bla bla ...
},
many more functions ...
//Execute steps one by one
interval = setInterval(executeRequestsStepByStep, 50);
function executeRequestsStepByStep() {
if (loadInProgress == false && typeof steps[testindex] == "function") {
//console.log("step " + (testindex + 1));
steps[testindex]();
testindex++;
}
if (typeof steps[testindex] != "function") {
console.log("Script is completed...");
phantom.exit();
}
}
page.onError = function(msg, trace) {
var msgStack = ['ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function+'")' : ''));
});
}
console.error(msgStack.join('\n'));
phantom.exit(2); //this is needed in case any error related to phantom script occurs.
};
在上面的代码示例中,当我执行脚本时,我在步骤1和步骤5中得到类似这样的错误,这是由于某个时间点页面上的一些javascript代码执行造成的。
ERROR: TypeError: undefined is not a constructor (evaluating 'sf_chat_Widget.loc
ale.includes("CH")')
TRACE:
-> https://e-com.secure.force.com/adidasContact/servlet/servlet.FileDownload?fi
le=01520000003gdry: 58 (in function "loadWidget")
-> http://www.adidas.co.uk/: 3367 (in function "onload")
phantomjs://code/UkScript.js:158 in onError
我希望忽略或跳过此错误而不停止脚本中的步骤执行,并且只捕获由未发生的幻像JS脚本引起的错误,并且在页面上发生所有错误。
是否已经有办法跳过或忽略特定的页面错误和 只是捕捉幻影js脚本中发生的错误?
P.S。我是Stackoverflow的新手,在提问时如果你发现任何不相关的内容或者之前已经被问过,请不要犹豫。 非常感谢您的帮助!