问题描述
尝试使用CasperJS抓取网页.网页会检查浏览器是否为IE 6/7.
Trying to scrape a web page with CasperJS. Webpage checks to see if the browser is an IE 6/7.
使用casperjs传递userAgent似乎不满足其条件.通过的UserAgent:Mozilla/4.0(兼容; MSIE 6.0; Windows NT 5.1)以下是页面为确定浏览器而进行的检查
Passing an userAgent with casperjs doesn't seem to satisfy its condition. UserAgent passed: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)Following is the check being made by the page to determine the browser
agt = navigator.userAgent.toLowerCase();
browserType = navigator.appName;
if( ((browserType.indexOf("xplorer") != -1)
&& (agt.indexOf("msie 6.") != -1))
|| ((browserType.indexOf("xplorer") != -1)
&& (agt.indexOf("msie 7.") != -1)) )
{
}
else
{
alert("This "+ browserType + " Version is not supported by this application. Please use Internet Explorer 6.x or Internet Explorer 7.x.");
window.close();
}
以下是casperjs的调试信息.
Following is the debug info from casperjs.
[警告] [虚拟]加载资源失败,状态为失败(HTTP 200):http://
[warning] [phantom] Loading resource failed with status=fail (HTTP 200): http://
在页面重定向之前是否有任何设置window.navigator
对象的指针?
Any pointers on setting window.navigator
object before page redirect?
推荐答案
navigator
属性是只读的,因此您无法设置它们,并且PhantomJS不提供设置它的功能.
The navigator
properties are read only, so you cannot set them and PhantomJS doesn't provide a capability to set it.
解决方案是制作navigator
对象的代理.旧的navigator
保留在后台,但是由行为相同的新替换,但替换为appName
的"Internet Explorer".整个引导过程可以从 page.initialized
回调.
casper.on('page.initialized', function(){
this.evaluate(function(){
(function(oldNav){
var newNav = {};
[].forEach.call(Object.getOwnPropertyNames(navigator), function(prop){
if (prop === 'appName') {
Object.defineProperty(newNav, prop, {
enumerable: false,
configurable: false,
writable: false,
value: 'Internet Explorer'
});
} else {
Object.defineProperty(newNav, prop, {
enumerable: false,
configurable: false,
get: function(){
return oldNav[prop];
}
});
}
});
window.navigator = newNav;
})(window.navigator);
});
});
使用page.onInitialized
事件处理程序的香草PhantomJS也是一样.
The same goes for vanilla PhantomJS with the page.onInitialized
event handler.
解决浏览器检测问题并不能保证页面在PhantomJS上正常工作或看起来不错.某些原因是针对IE优化"了某些页面,原因是大多数情况下使用的某些专有功能是其他浏览器所不具备的.
这篇关于CasperJS无法设置window.navigator对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!