Does changing the user-agent setting in PhantomJS change the perspective of the screen capture/screenshot?

穿精又带淫゛_ 提交于 2019-12-24 07:41:03

问题


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

Link to HTML/CSS code used and referenced below

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.

** 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!


回答1:


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 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.



来源:https://stackoverflow.com/questions/39680316/does-changing-the-user-agent-setting-in-phantomjs-change-the-perspective-of-the

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