Specify the viewport size for PhantomJS as a command line argument

Can I specify a viewport size for PhantomJS as a command line argument?

+5
source share
2 answers

You can try the following:

var args = require('system').args,
    viewportSize = {width: 1280, height: 1024},
    page = require('webpage').create();

if (args.length >= 1) {
  args.forEach(function(arg, i) {
    if (arg.match(/^[0-9]+x[0-9]+$/)) {
      var viewportParts = arg.split('x');
      viewportSize = {width: viewportParts[0], height: viewportParts[1]};
    }
  });
}
page.viewportSize = viewportSize;

Then run the script and add 320x480 after the script name, for example ...

+2
source

Of course. You will have to process the command line arguments yourself using system.args, which look for your custom option viewportor what you decide to call, and pluck its value. Then set page.viewportSize to the appropriate value using the supplied argument value.

+1
source

All Articles