Javascript 如何使用phantomjs呈现html元素

Javascript 如何使用phantomjs呈现html元素,javascript,phantomjs,Javascript,Phantomjs,我想将图像保存在代码中指定的div中。但是使用下面的代码,我得到了一些其他部分的渲染。这是正确的方法吗?我只是phantomjs的初学者。所以请帮助 var page = require('webpage').create(); page.open("http://n1k0.github.io/casperjs/#phantom_Casper_captureSelector", function (status) { if (status !== 'success') {

我想将图像保存在代码中指定的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);
    }
});

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

改变

var clipRect = page.evaluate(function () { 
return document.querySelector(".span7 demo").getBoundingClientRect(); });
致:

编辑 好的,我想弄明白这一点,下面是适合我的代码。我不熟悉使用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);
    }
});

我相信您应该在这里使用JSON.stringify来包围返回对象

return JSON.stringify(document.getElementsByClassName("span7 demo")[0].getBoundingClientRect();
或者获取div的内容

return JSON.stringify(document.getElementsByClassName("span7 demo")[0].innerHTML); 

您也可以轻松捕获具有ID的元素。只需将
getElementsByCassName(“classname”)[0]
替换为
document.getElementById('divID')
。完整的工作示例如下:

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

page.open("https://www.sejlar.com/maps.html", function (status) {
    if (status !== 'success') {
        console.log('Page not found');
    } 
    else {
        var p = page.evaluate(function () {
            return document.getElementById('map').getBoundingClientRect();
        });

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

        page.render('screenshot.png');
        phantom.exit(); 
    }
});

它看起来很简单,快速搜索会给你这样一个线索:@DanielFigueroa谢谢你的链接。我仍然无法在我的代码中找到错误。实际上我得到了一部分呈现。但是从同一页面的其他部分剪切。它现在显示了一个错误。TypeError:'null'不是使用var clipRect=page.evaluate(函数()的对象){return document.querySelector(“.span7 demo”).getBoundingClientRect();});我得到了png输出。但我没有得到我想要的div。是的,我以前的回答只是废话,对此我很抱歉。但我已经为你提供了一个适合我的例子。我重复使用了代码,我在www.google.com上试着提取div lga。这不起作用。有什么想法吗?
var page = require('webpage').create();

page.open("https://www.sejlar.com/maps.html", function (status) {
    if (status !== 'success') {
        console.log('Page not found');
    } 
    else {
        var p = page.evaluate(function () {
            return document.getElementById('map').getBoundingClientRect();
        });

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

        page.render('screenshot.png');
        phantom.exit(); 
    }
});