PhantomJs不会调整窗口大小

时间:2017-02-26 22:59:39

标签: javascript phantomjs

我正在使用此代码: var width = 1024; var height = 768;

和page.viewportSize = {width:width,height:height};我在page.open之前使用。但这不起作用。 您可以在我的另一个问题中查看完整代码 - Faking the Referer Header in PhantomJS is doesn't work

这是代码:

var width = 1024;
var height = 768;
var page = require('webpage').create();
//referal massive
var reff = ["https://www.facebook.com/messages/t/","https://google.com","https://youtube.com","https://twitter.com/"];
//random massive
var randreff = Math.floor(Math.random() * (reff.length));
//reffer
page.customHeaders = {
  "Referer": (reff[randreff])
};
//console refer
console.log(reff[randreff]);
var urls = ['http://test.com/','http://test.com/2017/02/blog-post.html','http://test.com/'];
var i = 0;
function OpenPage(){
    setTimeout(function(){
            page.open(urls[i], function(status) {
                    if (status == 'success') {
                page.viewportSize = {width: width, height: height};
                    page.render('example' + i + '.png');
            }
            i++;
            if(i <= urls.length - 1){ 
                OpenPage();
            }else{
               phantom.exit();
            }
        });
    },5000);
}
OpenPage();
  • 分辨率不起作用且推介不起作用=(

1 个答案:

答案 0 :(得分:2)

无论视口大小如何,

page.render()都会渲染整个页面,这是正常的行为......您需要的是page.clipRect() ...类似于:

page.viewportSize = {width: width, height: height};

上面应该在page.open()之前定义 - 告诉页面呈现方式,就像窗口的大小一样

要渲染给定大小的图像,您需要:

page.clipRect = {
    top: 0,
    left: 0,
    height: viewportSize.height,
    width: viewportSize.width
  };

page.render(...);