Set screen resolution in CapserJS/SlimerJS

♀尐吖头ヾ 提交于 2019-12-19 05:07:38

问题


Im using CasperJS 0.10.1 SlimerJS 1.1.3 Firefox 45 on CentOS 7.2 Im trying to set the window.screen properties as seen by the code below by the screenshot of the website still says 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:


You can set the viewport size:

casper.viewport(1600, 900);

Or even more:

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

it's just simple realization of the navigator object: plugins could look better, but usually it's not needed.



来源:https://stackoverflow.com/questions/40311430/set-screen-resolution-in-capserjs-slimerjs

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