javascript - PhantomJS 获取特定元素的位置数组
问题描述
我想使用 phantomjs 获取页面上特定标签的位置。例如对于<a>
我想得到这个数组的标签:
[
{
tag: "a",
x: 12,
y: 32,
width: 100,
height: 30
},
...
]
我写了这段代码:
page.open(url, function(status){
....
....
var a_tags = page.evaluate(function() {
return document.getElementsByTagName('a');
});
for(index in a_tags){
console.log(a_tags[index].getBoundingClientRect());
}
....
....
})
但是此代码返回错误:
TypeError: null is not a function (evaluating 'a_tags[index].getBoundingClientRect()')
如何使用 phantomjs 从页面获取这些信息,我的代码有什么问题?
解决方案
我找到了解决方案:
var a_tags = page.evaluate(function() {
var tags = document.getElementsByTagName('a');
var res = [];
for(var index = 0 ; index < tags.length ; index++) {
var element = tags[index];
var rect = element.getBoundingClientRect();
if(rect.left == 0 && rect.top ==0 || rect.width ==0 && rect.height ==0){
continue;
}
res.push({
tag: "a",
rect: rect,
});
}
return res;
});
推荐阅读
- heroku - 如何在 heroku 上启用 gatsbyjs 客户端专用路由
- twilio - 通过 SIP 将物理 IP 电话与 Twilio 链接
- c++ - 链接两个文件
- javascript - 外部 Javascript 不起作用,它被 Atom 显示为灰色。它们在同一个文件夹中,它只是 Atom 还是我只是愚蠢?
- c# - 在这种情况下不允许异步操作
- vector - 如何从 Rust 中的结构数组中获取字段值数组?
- django - 在django中通过前端向数据库添加数据
- firebase - 我可以在 Firebase 中使用 DateTime.now().toString() 作为 Flutter 的孩子吗
- sql - Oracle SQL 查询从 csv 文件中获取数据到 IN 条件中
- python - 从 Github 克隆后运行 Django 项目不起作用?