如何调试 PhantomJS 脚本?
How to debug a PhantomJS script?
我的脚本有一些语法错误,但 PhantomJS 没有显示任何错误,而是没有显示任何内容。如果脚本有错误,为什么 Phantom JS 不显示解析错误?
在下面的 PhantomJS 脚本中(运行 通过 windows CMD),如果脚本中存在解析错误,phantomJs 会挂起而不是显示错误。
var system = require('system');
var webpage = require('webpage').create();
console.log('starting script');
if (system.args.length === 0) {
console.log('no args');
} else {
system.args.forEach(function(arg,index){
console.log('arg is '+arg+' at '+index);
});
}
webpage.open('http://localhost:3000/cookie-demo',function(status){
if (status === 'success'){
console.log('success in opening page');
phantom.cookies.forEach(function(cookie,index){
for ( var key in cookie){
/*if instead of index, I use i as variable (undefined), the script just hangs!*/
console.log('[cookie:'+index+']'+key+'='+'cookie[key]');
}
});
phantom.exit(0);
}
else{
console.log('could not open the page');
phantom.exit(1);
}
});
如果脚本中没有语法错误,我会得到以下输出
C:\Users\Manu\Documents\manu\programs\random>phantomjs --cookies-file=cookie-jar.txt phantomTest.js
starting script
arg is phantomTest.js at 0
success in opening page
[cookie:0]domain=cookie[key]
[cookie:0]expires=cookie[key]
[cookie:0]expiry=cookie[key]
[cookie:0]httponly=cookie[key]
[cookie:0]name=cookie[key]
[cookie:0]path=cookie[key]
[cookie:0]secure=cookie[key]
[cookie:0]value=cookie[key]
[cookie:1]domain=cookie[key]
[cookie:1]expires=cookie[key]
[cookie:1]expiry=cookie[key]
[cookie:1]httponly=cookie[key]
[cookie:1]name=cookie[key]
[cookie:1]path=cookie[key]
[cookie:1]secure=cookie[key]
[cookie:1]value=cookie[key]
但是如果出现语法错误,我在控制台上什么也看不到,也不会退出
C:\Users\Manu\Documents\manu\programs\random>phantomjs --cookies-file=cookie-jar.txt phantomTest.js
如果存在语法错误,PhantomJS 2.0 和 2.1 版将自动失败并挂起。使用 1.9.8 或 2.5 测试版。所有下载都在这里:https://bitbucket.org/ariya/phantomjs/downloads/
最好还是迁移到 Puppeteer,这是一个与 API 非常相似的项目,在底层使用无头 Google Chrome。
我的脚本有一些语法错误,但 PhantomJS 没有显示任何错误,而是没有显示任何内容。如果脚本有错误,为什么 Phantom JS 不显示解析错误?
在下面的 PhantomJS 脚本中(运行 通过 windows CMD),如果脚本中存在解析错误,phantomJs 会挂起而不是显示错误。
var system = require('system');
var webpage = require('webpage').create();
console.log('starting script');
if (system.args.length === 0) {
console.log('no args');
} else {
system.args.forEach(function(arg,index){
console.log('arg is '+arg+' at '+index);
});
}
webpage.open('http://localhost:3000/cookie-demo',function(status){
if (status === 'success'){
console.log('success in opening page');
phantom.cookies.forEach(function(cookie,index){
for ( var key in cookie){
/*if instead of index, I use i as variable (undefined), the script just hangs!*/
console.log('[cookie:'+index+']'+key+'='+'cookie[key]');
}
});
phantom.exit(0);
}
else{
console.log('could not open the page');
phantom.exit(1);
}
});
如果脚本中没有语法错误,我会得到以下输出
C:\Users\Manu\Documents\manu\programs\random>phantomjs --cookies-file=cookie-jar.txt phantomTest.js
starting script
arg is phantomTest.js at 0
success in opening page
[cookie:0]domain=cookie[key]
[cookie:0]expires=cookie[key]
[cookie:0]expiry=cookie[key]
[cookie:0]httponly=cookie[key]
[cookie:0]name=cookie[key]
[cookie:0]path=cookie[key]
[cookie:0]secure=cookie[key]
[cookie:0]value=cookie[key]
[cookie:1]domain=cookie[key]
[cookie:1]expires=cookie[key]
[cookie:1]expiry=cookie[key]
[cookie:1]httponly=cookie[key]
[cookie:1]name=cookie[key]
[cookie:1]path=cookie[key]
[cookie:1]secure=cookie[key]
[cookie:1]value=cookie[key]
但是如果出现语法错误,我在控制台上什么也看不到,也不会退出
C:\Users\Manu\Documents\manu\programs\random>phantomjs --cookies-file=cookie-jar.txt phantomTest.js
如果存在语法错误,PhantomJS 2.0 和 2.1 版将自动失败并挂起。使用 1.9.8 或 2.5 测试版。所有下载都在这里:https://bitbucket.org/ariya/phantomjs/downloads/
最好还是迁移到 Puppeteer,这是一个与 API 非常相似的项目,在底层使用无头 Google Chrome。