If I have know a id of element is "footer" ,I can use
如果我知道元素的id是“页脚”,我可以使用
document.getElementById("footer").getBoundingClientRect();
to get the "footer" element coordinates.
获取“页脚”元素坐标。
There is all the code
有所有的代码
var page = require('webpage').create();
page.open("https://*.com/questions/18657615/how-to-render-an-html-element-using-phantomjs", 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.body.getBoundingClientRect();
return document.getElementById("footer").getBoundingClientRect();
});
page.clipRect = {
top: bb.top,
left: bb.left,
width: bb.width,
height: bb.height
};
console.log(bb.top);
console.log(bb.left);
console.log(bb.width);
console.log(bb.height);
page.render('capture.png');
phantom.exit();
}, 200);
}
});
the result is
结果是
4004.6875
0
1075
632.5
4004.6875 0 1075 632.5
I must know the page have element that id is "footer". if there is a unknown webpage, I do not know any info of the webpage. How I can get all the element coordinates.
我必须知道页面中有id为“footer”的元素。如果有一个未知的网页,我不知道该网页的任何信息。我如何获得所有元素坐标。
Maybe traversing the dom can help, but I always get errors. I do not know how to merge the code correctly.
也许穿越dom可以提供帮助,但我总是会遇到错误。我不知道如何正确合并代码。
1 个解决方案
#1
2
Let's see how we can change the code given in the link you've found:
让我们看看我们如何更改您找到的链接中给出的代码:
function theDOMElementWalker(node) { if (node.nodeType == 1) { //console.log(node.tagName); node = node.firstChild; while (node) { theDOMElementWalker(node); node = node.nextSibling; } } }
This can be easily extended to "copy" the DOM to a custom representation. For example:
这可以很容易地扩展到“复制”DOM到自定义表示。例如:
var dom = page.evaluate(function(){
var root = { children: [] };
function walk(node, obj) {
if (node.nodeType == 1) {
obj.tagName = node.tagName;
obj.boundingClientRect = node.getBoundingClientRect();
node = node.firstChild;
var childObj;
while (node) {
childObj = { children: [] };
obj.children.push(childObj);
walk(node, childObj);
node = node.nextSibling;
}
}
}
walk(document.documentElement, root);
return root;
});
console.log(JSON.stringify(dom, undefined, 4));
The idea here is to pass in the DOM node and its "simple" representation into the walk function.
这里的想法是将DOM节点及其“简单”表示传递给walk函数。
#1
2
Let's see how we can change the code given in the link you've found:
让我们看看我们如何更改您找到的链接中给出的代码:
function theDOMElementWalker(node) { if (node.nodeType == 1) { //console.log(node.tagName); node = node.firstChild; while (node) { theDOMElementWalker(node); node = node.nextSibling; } } }
This can be easily extended to "copy" the DOM to a custom representation. For example:
这可以很容易地扩展到“复制”DOM到自定义表示。例如:
var dom = page.evaluate(function(){
var root = { children: [] };
function walk(node, obj) {
if (node.nodeType == 1) {
obj.tagName = node.tagName;
obj.boundingClientRect = node.getBoundingClientRect();
node = node.firstChild;
var childObj;
while (node) {
childObj = { children: [] };
obj.children.push(childObj);
walk(node, childObj);
node = node.nextSibling;
}
}
}
walk(document.documentElement, root);
return root;
});
console.log(JSON.stringify(dom, undefined, 4));
The idea here is to pass in the DOM node and its "simple" representation into the walk function.
这里的想法是将DOM节点及其“简单”表示传递给walk函数。