Casperjs有一些问题,因为它最初加载时没有验证页面,下面是我的代码。
var casper = require('casper').create({
verbose: false,
logLevel: 'debug',
pageSettings: {
loadImages: false, // The WebPage instance used by Casper will
loadPlugins: false, // use these settings
}
});
// print out all the messages in the headless browser context
casper.on('remote.message', function(msg) {
this.echo('remote message caught: ' + msg);
});
// print out all the messages in the headless browser context
casper.on("page.error", function(msg, trace) {
this.echo("Page Error: " + msg, "ERROR");
});
//casper.options.viewportSize = {width: 1366, height: 667};
casper.start();
casper.options.pageSettings = {
customHeaders:{
'Authorization':'Basic '+btoa('username:password')
}
}
casper.thenOpen('https://www.dmr.nd.gov/oilgas/basic/getwellprod.asp', function(status) {
if (status !== 'success') {
console.log('Unable to access network');
} else {
console.log("Getting Authenticated");
this.echo(this.getTitle());
}
});
casper.run();
我也尝试过使用setHttpAuth而没有运气。这里有Phantom版本和resource.error消息 - 幻影版本是1.9.2,ErrorCode是6,描述是SSL握手失败
答案 0 :(得分:2)
来自resource.error
的错误消息表明它是一个POODLE错误。 PhantomJS< 1.9.8默认使用SSLv3,但由于POODLE,许多Web服务器禁用了SSLv3支持。所以你需要告诉PhantomJS / CasperJS应该使用TLS:
--ssl-protocol=tlsv1
您也可以将--ignore-ssl-errors=true
投入使用。
此处提供更多信息:CasperJS/PhantomJS doesn't load https page
pageSettings
没有customHeaders
选项。你可能想要使用PhantomJS' customHeaders
选项:
casper.page.customHeaders:{
'Authorization': 'Basic '+btoa('username:password')
};
更好的方法是使用适当的设置:
casper.options.pageSettings = {
userName: username,
password: password
};
您还可以在创建过程中定义它。
如果这不能解决您的问题,请检查resource.requested
和resource.received
事件是否已设置标头。
您的代码的另一个问题是status
和其他步骤函数的回调没有thenOpen
。最后成功加载的资源对象将传递给回调而不是字符串。因此,您无法使用该功能检测页面是否已成功加载。