在CapserJS / SlimerJS中设置屏幕分辨率

时间:2016-10-28 18:48:00

标签: casperjs slimerjs

我在CentOS 7.2上使用CasperJS 0.10.1 SlimerJS 1.1.3 Firefox 45我试图设置window.screen属性,如下面的代码所示,网站截图仍然说640x480

var casper = require('casper').create({ verbose: true, logLevel: 'debug' });
casper.on('page.initialized', function (page) {
    page.evaluate(function () { 
        (function() {
            window.screen = {
                width: 1600,
                height: 900
            };
        })
    });
});
casper
  .start()
  .thenOpen('http://www.whatismyscreenresolution.com/')  
  .wait(5000, function() { this.capture('/cas/_test_screenres.jpg',{top:0,left:0,width:1600,height:900}); })
  .run();

1 个答案:

答案 0 :(得分:2)

您可以设置视口大小:

casper.viewport(1600, 900);

甚至更多:

function on_init (page){
page.viewportSize = {width:1600,height:900}
page.evaluate(function (){
window.screen = {width:1600,height:900,availWidth:1600,availHeight:900};
window.innerWidth=1600;  window.innerHeight=900;   window.outerWidth=1600;  window.outerHeight=900;
window.navigator = {
plugins: {length: 2, 'Shockwave Flash': {name: 'Shockwave Flash', filename: '/usr/lib/flashplugin-nonfree/libflashplayer.so', description: 'Shockwave Flash 11.2 r202', version: '11.2.202.440'}},
mimeTypes: {length: 2, "application/x-shockwave-flash": {description: "Shockwave Flash", suffixes: "swf", type: "application/x-shockwave-flash", enabledPlugin: {name: 'Shockwave Flash', filename: '/usr/lib/flashplugin-nonfree/libflashplayer.so', description: 'Shockwave Flash 11.2 r202', version: '11.2.202.440'}}},
appCodeName: "Mozilla",
appName: "Netscape",
appVersion: "5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36",
cookieEnabled: 1,
languages: "en-US,en",
language: "en",
onLine: 1,
doNotTrack: null,
platform: "Linux x86_64",
product: "Gecko",
vendor: "Google Inc.",
vendorSub: "",
productSub: 20030107,
userAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36",
geolocation: {getCurrentPosition: function getCurrentPosition(){},watchPosition: function watchPosition(){},clearWatch: function clearWatch(){}},
javaEnabled: function javaEnabled(){return 0} };});};

casper.on('page.initialized', on_init);

只是navigator对象的简单实现:plugins看起来更好,但通常不需要。