更改PhantomJS中的用户代理设置是否会更改屏幕截图/屏幕截图的视角? [英] Does changing the user-agent setting in PhantomJS change the perspective of the screen capture/screenshot?

查看:86
本文介绍了更改PhantomJS中的用户代理设置是否会更改屏幕截图/屏幕截图的视角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用PhantomJS还是比较陌生,并且想获取我们网站的屏幕截图,以便稍后使用.我很好奇的是,更改用户代理设置是否会影响屏幕快照的视角(即图像外观是否会有所不同)? -如果不能,您如何建议我能够拍摄模拟不同浏览器的屏幕截图(希望使用Phantom,但愿意接受建议)?

I am relatively new to using PhantomJS, and would like to take screenshots of our website to be diffd later on. What I am curious to know is whether or not changing the user-agent setting will affect the perspective of the screenshot (i.e. will the image appear different)? - If it will not, how would you suggest me being able to take screenshots emulating different browsers (hopefully using Phantom, but open to suggestions)?

// PhantomJS Code
page = require('webpage').create();
// Used for Firebird (old Firefox)
page.settings.userAgent = 'Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.5) Gecko/20031007 Firebird/0.7';
// Used for Chrome
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36';
page.viewportSize = {width: 1200, height : 800};
page.open("webAddress", function() {
    page.render(picName);
    pahntom.exit();
});

链接到HTML/CSS

是的,我已经通过更改用户代理属性进行了测试,并已截取屏幕截图进行确认(上面的代码).我们的网站定义明确,通常不会因浏览器而异,因此这是预防措施. -我还试图进行更改,以故意使网站在不同的浏览器中使用时看起来有所不同.无论我进行了什么更改,PhantomJS仍然可以用相同的方式捕获它们.

Yes, I have tested by changing the user agent attribute, and have taken screenshots to confirm (code above). Our site is rathe well defined, and does not generally change from browser to browser, so this is more of precautionary action. - I have also tried to make changes that would deliberately cause the site to look different when used in different browsers. They still appear to captured the same way by PhantomJS no matter what I change.

**免责声明:如前所述,我对PhantomJS还是比较陌生.我已经阅读了许多文档和相关文章,但还没有真正找到我的问题的答案.我也是一个新的Web开发人员(做了很多后端编程,所以我不一定是新手程序员),所以也许我的故意"更改是不正确的?或者,也许我没有以正确的方式使用user-agent属性?

** Disclaimer: As noted, I am relatively new to PhantomJS. I have read through much of the documentation, and related posts, but have not really found an answer to my question yet. I am also a new web developer (have done much backend programming so I'm not necessarily a newbie programmer) so perhaps my "deliberate" changes were incorrect? Or, perhaps I'm not using the user-agent attribute in the correct manner?

请让我知道我是否可以更具体. -任何/所有帮助将不胜感激!

Please let me know if I could be more specific. - Any/all help would be greatly appreciated!

推荐答案

当然可以出现不同.默认情况下,某些Web服务器以固定宽度的页面进行响应.如果专门设置了Web服务器已知的用户代理字符串,并且该字符串表示移动浏览器,则可能会获得页面的屏幕优化版本.

It certainly can appear differently. Some web servers respond with a fixed-width page by default. If you specifically set a user agent string that is known to the web server and which denotes a mobile browser, you might get a small screen-optimized version of the page.

PhantomJS的默认视口为400 x 300像素.这意味着页面的移动版本可能会按预期呈现.固定宽度的页面可以增加该视口的大小.

PhantomJS has a default viewport of 400 x 300 pixels. Which means that mobile versions of pages are likely rendered as expected. Fixed width pages can increase that viewport size.

根据用户代理字符串,并非所有站点的反应都不同.请记住,如果您在执行多个脚本时截取屏幕截图而不进行任何更改,则页面的外观可能会有所不同,因为某些页面和广告会频繁更改.

Not all sites react differently depending on the user agent string. Keep in mind that the page will likely look differently if you're taking screenshots during multiple script executions without changing anything, because some pages as well as ads change frequently.

这篇关于更改PhantomJS中的用户代理设置是否会更改屏幕截图/屏幕截图的视角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆