CasperJS cannot set window.navigator object

谁说胖子不能爱 提交于 2019-12-13 20:13:49

问题


Trying to scrape a web page with CasperJS. Webpage checks to see if the browser is an IE 6/7.

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();
}

Following is the debug info from casperjs.

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

[warning] [phantom] Loading resource failed with status=fail (HTTP 200): http://

Any pointers on setting window.navigator object before page redirect?


回答1:


The navigator properties are read only, so you cannot set them and PhantomJS doesn't provide a capability to set it.

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);
    });
});

The same goes for vanilla PhantomJS with the page.onInitialized event handler.

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.



来源:https://stackoverflow.com/questions/27523682/casperjs-cannot-set-window-navigator-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!