CasperJS getElementsByXPath只返回第一个元素 [英] CasperJS getElementsByXPath only returning first element

查看:518
本文介绍了CasperJS getElementsByXPath只返回第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码获取第一个表行中的所有表格单元格。我想检查每个表格单元格的innerHTML。但是在此函数返回的对象中,只有第一个表格单元格实际存在,所有其他属性都为空:

I use the following code to get all table cells in the first table row. I'd like to then check the innerHTML of every single table cell. But in the object returned by this function only the first table cell is actually there, all the other properties are null:

firstRow = this.evaluate(function () {
    return __utils__.getElementsByXPath('//tbody/tr[1]/td');
});

utils.dump(firstRow);

utils.dump的输出是:

The output from utils.dump is:

[
    {
        "abbr": "",
        "align": "",
        "attributes": {...}
    },
    null,
    null,
    null
]

我也试过 utils .findAll,它也是一样的。如何获得所有匹配的元素?

I also tried with utils.findAll and it was the same. How can I get all the matched elements?

推荐答案

使用Casper / PhantomJS evaluate()函数,你必须将原生DOM元素和元素列表映射到JSON可序列化的东西:

With Casper/PhantomJS evaluate() functions, you have to map native DOM elements and lists of elements to something JSON-serializable:

var firstRow = this.evaluate(function () {
    var elements = __utils__.getElementsByXPath('//tbody/tr[1]/td');
    return [].map.call(elements, function(element) {
        return element.outerHTML;
    });
});

utils.dump(firstRow);

这篇关于CasperJS getElementsByXPath只返回第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆