CasperJS无法设置window.navigator对象 [英] CasperJS cannot set window.navigator object

查看:107
本文介绍了CasperJS无法设置window.navigator对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用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.

[信息] [远程] [警告]此应用程序不支持此Netscape版本 在.请使用Internet Explorer 6.x或Internet Explorer 7.x.

[info] [remote] [alert] This Netscape Version is not supported by this applicat on. Please use Internet Explorer 6.x or Internet Explorer 7.x.

[警告] [虚拟]加载资源失败,状态为失败(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 回调.

The solution is to make a proxy of the navigator object. The old navigator stays in the background, but it is replaced with a new one that behaves the same, but with an appName of "Internet Explorer". This whole bootstrapping process can be triggered from the page.initialized callback.

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优化"了某些页面,原因是大多数情况下使用的某些专有功能是其他浏览器所不具备的.

Working around the browser detection doesn't guarantee that the page works or looks good on PhantomJS. There is a reason some pages are "optimized" for IE and the reason is most of the time that some propietary features where used that are not there in other browsers.

这篇关于CasperJS无法设置window.navigator对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆