如何使用phantomjs呈现html元素

Ser*_*fka 10 javascript phantomjs

我想将图像保存在代码中指定的div内.但是使用下面的代码我会得到一些其他的部分.这是正确的方法吗?我只是phantomjs的初学者.所以请帮助.

var page = require('webpage').create();

page.open("http://n1k0.github.io/casperjs/#phantom_Casper_captureSelector", function    (status) {
    if (status !== 'success') {
        console.log('Unable to load the address!');
    } else {

        var clipRect = page.evaluate(function () { 
        return document.querySelector(".span7 demo").getBoundingClientRect(); });
        page.clipRect = {
            top:    clipRect.top,
            left:   clipRect.left,
            width:  clipRect.width,
            height: clipRect.height
        };



        window.setTimeout(function () {
            page.render('capture.png');
            phantom.exit();
        }, 200);
    }
});
Run Code Online (Sandbox Code Playgroud)

Dan*_*roa 12

这可能是完全错误的,但我在评论中提供的链接是这样的:

更改

var clipRect = page.evaluate(function () { 
return document.querySelector(".span7 demo").getBoundingClientRect(); });
Run Code Online (Sandbox Code Playgroud)

至:

var clipRect = document.querySelector(".span7 demo").getBoundingClientRect(); });
Run Code Online (Sandbox Code Playgroud)

编辑

好的,所以我想想出这个,这里是适合我的代码.我不熟悉使用querySelector的phantomjs api 所以我最终使用的getElementsByClassName是几乎相同的.

var page = require('webpage').create();

page.open("http://n1k0.github.io/casperjs/#phantom_Casper_captureSelector", function    (status) {
    if (status !== 'success') {
        console.log('Unable to load the address!');
    } else {
        window.setTimeout(function () {
            //Heres the actual difference from your code...
            var bb = page.evaluate(function () { 
                return document.getElementsByClassName("span7 demo")[0].getBoundingClientRect(); 
            });

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

            page.render('capture.png');
            phantom.exit();
        }, 200);
    }
});
Run Code Online (Sandbox Code Playgroud)