javascript - CasperJS 屏幕截图显示空白屏幕

标签 javascript phantomjs casperjs

我一直在用这个敲我的头。

我使用 phantomjs 1.9.8 在 casperjs 1.1.0-beta3 上运行

我正在尝试从 url 捕获图像并将其保存为文件。

这是我的命令:

casperjs --proxy-type=none --ssl-protocol=any /home/casper-capture.js http://url.com/demo/demo /home/demoScreenshot.png

这是我的casper-capture.js

/**
* capture image from url and save it to file.
**/
var casper = require('casper').create({
    verbose: true,
    logLevel: "debug",
     viewportSize: {
        width: 2300,
        height: 1200
    },
    pageSettings: {
        webSecurityEnabled: false,
        loadImages:  true,        // The WebPage instance used by Casper will
        loadPlugins: true         // use these settings
    }
});
// delay before image capturing
var delay = 60000;
//timeout delay for loading
var timeoutForLoading = 10 * 60000;
// image source url.
var url = casper.cli.args[0];
// image output path
var path = casper.cli.args[1];

casper.start().zoom(4).thenOpen(url, function urlCaptureClouser() {
    this.wait(delay, function(){
        casper.waitFor(function check() {
            return this.evaluate(function() {
                return document.querySelectorAll('.fa-spin').length +
                        document.querySelectorAll('.chart-loading').length == 0;
            });
        }, function then() {
           this.capture(path);
        }, function then() {
            this.capture(path);
        }, timeoutForLoading);
    });
});

casper.run();

我遇到“空白”屏幕。 :-(

当我在本地系统上运行它时。有用!我不确定我做错了什么。

有人可以指出我的问题吗?

控制台日志(带有错误日志记录):

[info] [phantom] Starting...
[info] [phantom] Running suite: 2 steps
[debug] [phantom] opening url: <URL>, HTTP GET
[debug] [phantom] Navigation requested: url=<URL>, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "<URL>"
Error: TypeError: 'undefined' is not an object (evaluating 'Object.assign.apply')
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step urlCaptureClouser 2/2 <URL> (HTTP 200)
[info] [phantom] Step urlCaptureClouser 2/2: done in 329ms.
[info] [phantom] Step _step 3/3 <URL> (HTTP 200)
[info] [phantom] Step _step 3/3: done in 351ms.
[info] [phantom] wait() finished waiting for 60000ms.
[info] [phantom] Step _step 4/4 <URL> (HTTP 200)
[info] [phantom] Step _step 4/4: done in 60358ms.
[info] [phantom] waitFor() finished in 41ms.
[info] [phantom] Step then 5/5 <URL> (HTTP 200)
[debug] [phantom] Capturing page to /home/dmeo1991.png
[info] [phantom] Capture saved to /home/dmeo1991.png
[info] [phantom] Step then 5/5: done in 60674ms.
[info] [phantom] Done 5 steps in 60674ms
Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file:///usr/lib/node_modules/casperjs/bin/bootstrap.js. Domains, protocols and ports must match.

最佳答案

您的日志显示“错误:类型错误:‘未定义’不是对象(正在评估‘Object.assign.apply’)”,这可能会导致黑屏,因为实际上没有执行任何 JavaScript。

您需要upgrade to PhantomJS 2.x (PhantomJS 1.x 背后的引擎现在已经有 5 年多了)或者添加一个 polyfill。我已经从 MDN 复制了polyfill :

casper.on('page.initialized', function(){
    this.evaluate(function(){
        // Polyfill...
        if (typeof Object.assign != 'function') {
          Object.assign = function(target) {
            'use strict';
            if (target == null) {
              throw new TypeError('Cannot convert undefined or null to object');
            }

            target = Object(target);
            for (var index = 1; index < arguments.length; index++) {
              var source = arguments[index];
              if (source != null) {
                for (var key in source) {
                  if (Object.prototype.hasOwnProperty.call(source, key)) {
                    target[key] = source[key];
                  }
                }
              }
            }
            return target;
          };
        }
    });
});

casper.start(...)...

关于javascript - CasperJS 屏幕截图显示空白屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38908484/

相关文章:

javascript - 如何更改 jquery easyPiechart 的条形颜色

java - 将 PhantomJS 二进制文件添加到 Maven 项目的更好方法?

phantomjs - 通过 npm windows 安装 casperjs 时不安全的 javascript 尝试访问框架

javascript - 如何在CasperJS中顺序打开2个网页?

javascript - 具有旋转代理的 Phantomjs/Casperjs?

javascript - Chrome : Error in event handler for runtime. onMessage

javaScript 有没有更好的方法来制作这个动画?

http - Phantomjs 无法打开某些 URL

javascript - 如何将多个 JS promise 链接到组合的 bool 结果

javascript - phantomjs : Calling page. 从其他函数或模块求值