最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to render an html element using phantomjs - Stack Overflow

programmeradmin4浏览0评论

I want to save the image inside the div specified in the code. But using the code below i"m getting some other portion rendered. Is this the correct way to do it? I'm just a beginner in phantomjs. So Please help.

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

page.open("", 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);
    }
});

I want to save the image inside the div specified in the code. But using the code below i"m getting some other portion rendered. Is this the correct way to do it? I'm just a beginner in phantomjs. So Please help.

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);
    }
});
Share Improve this question edited Sep 6, 2013 at 12:42 Daniel Figueroa 10.7k5 gold badges47 silver badges68 bronze badges asked Sep 6, 2013 at 12:13 Serjical KafkaSerjical Kafka 3773 gold badges4 silver badges10 bronze badges 3
  • It looks lite it, a quick search gives you this SO thread: stackoverflow.com/questions/11917042/… – Daniel Figueroa Commented Sep 6, 2013 at 12:16
  • @DanielFigueroa Thanks for the link. Still i'm not able to find the error in my code. – Serjical Kafka Commented Sep 6, 2013 at 12:22
  • actually i'm getting a portion rendered. But cropped from some other portion of the same page. – Serjical Kafka Commented Sep 6, 2013 at 12:30
Add a comment  | 

3 Answers 3

Reset to default 13

This might be completely wrong but the link that I provided in my comment does it like this:

Change

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

to:

var clipRect = document.querySelector(".span7 demo").getBoundingClientRect(); });

EDIT

Okay so I wanted to figure this one out and here's the code that works for me. I'm not familiar with the phantomjs api to use querySelector so I ended up using getElementsByClassName which is pretty much the same.

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

You can easily capture an element with an ID too. Just replace getElementsByClassName("classname")[0] by document.getElementById('divID'). A full working example would be:

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

I believe what you should do here is surround your return-object with JSON.stringify.

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

or getting the contents of the div

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

评论列表(0)

  1. 暂无评论