Set Screen size in PhantomJS/CasperJS

随声附和 提交于 2019-12-10 19:10:14

问题


I'm automating access to a web site to collect data from it. Unfortunately that page detects the screen size and doesn't give me the desired page if the screen is too small. When running the script from a non-interactive context (IIS web application) the Screen object seems to return a size of 1024x768 pixels. Even though I set the viewport size to 1300x1000 pixels which affects screenshots but not the web page's JavaScript.

How can I tell PhantomJS or CasperJS (I'm using the latter but that's using the former) to return different values for the Screen object in page's JavaScript?

Edit: This is CasperJS, not PhantomJS, so it's not a duplicate.


回答1:


According official documentation you can do it using viewportSize option.

var casper = require("casper").create({
    // other options here
    viewportSize: {
        width: 1920,
        height: 1080
    }
}); 

It will be more lightweight solution than overriding viewport size after each page loading.




回答2:


Here's how it works with CasperJS:

// at the start of the script file
var casper = require("casper").create({
    // other options here
    onPageInitialized: function (page) {
        page.evaluate(function () {
            window.screen = {
                width: 1920,
                height: 1080
            };
        });
    }
});

// script goes on here



回答3:


Another option like you can see here:

casper.options.viewportSize = {width: 1600, height: 950};



回答4:


You can set by this as well while defining functions:

casper.viewport(1300, 1000).then(function () {
     test.assertUrlMatches(/testing\.html$/);
});


来源:https://stackoverflow.com/questions/37303343/set-screen-size-in-phantomjs-casperjs

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