【问题标题】:PhantomJS capturing screenshot of mobile browserPhantomJS 截取手机浏览器截图
【发布时间】:2015-12-05 01:26:17
【问题描述】:

我试图用 PhantomJS 创建一个网络截图,但我将图像作为移动浏览器获取。我正在使用 MAC OS 优胜美地。这是我的 JavaScript:

screen.js

var WebPage = require('webpage');
page = WebPage.create();
page.open('http://www.apple.com');
page.onLoadFinished = function() {
    window.setTimeout(function () {
        page.render('appleScreenShot' + '.png');
        phantom.exit();
    }, 2000);
}

这是我的命令行代码

phantomjs --ignore-ssl-errors=true --web-security=false --ssl-protocol=tlsv1 --debug=true screen.js

【问题讨论】:

    标签: javascript phantomjs screenshot


    【解决方案1】:

    您可能会发现需要为某些网站指定viewportSize(甚至可能是zoomFactor),具体取决于其资源中指定的媒体查询。

    来自viewportSize上的文档:

    因为 PhantomJS 是无头的(没有显示),viewportSize 像传统的一样有效地模拟窗口的大小 浏览器。

    示例用法:

    page.viewportSize = {
        width: 1280,
        height: 800
    };
    page.zoomFactor = 1; //default value is 1
    

    【讨论】:

    • 谢谢丹。设置 page.viewportSize 帮助了我。我使用了最大分辨率。 page.viewportSize = { 宽度:1600,高度:1200 };
    • 请您将该问题标记为正确答案。
    【解决方案2】:

    我建议你添加背景颜色渲染,因为默认是透明背景。

    page.evaluate(function() {
        document.body.bgColor = 'white';
    });
    

    【讨论】:

      【解决方案3】:

      您可以将 NightmareJs 与 node 一起使用:

      const Nightmare = require('nightmare');
      var nightmare = Nightmare({ show: false })
      nightmare
        .useragent('Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36')
        .viewport(1200, 1800)
        .wait()
        .goto('url...')
        .wait()
        .screenshot('image.png')
        .end()
        .then(function (result) {
          console.log('page done');
       })
        .catch(function (error) {
          console.error('Error:', error);
       })
      

      我发现它比 PhantomJs 更容易用于截图

      【讨论】:

        【解决方案4】:

        在 Java 中,我更改了用户代理字符串。它对我有用。

        DesiredCapabilities capabilities = new DesiredCapabilities();
                capabilities.setJavascriptEnabled(true);
                String userAgent = "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1";
         capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "userAgent", userAgent);
                    driver = new PhantomJSDriver(capabilities);
        

        【讨论】:

          猜你喜欢
          • 2013-06-15
          • 2015-06-10
          • 2019-11-28
          • 1970-01-01
          • 1970-01-01
          • 2022-01-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多