CasperJS getElementsByXPath only returning first element

冷暖自知 提交于 2019-12-04 11:26:12

问题


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

The output from utils.dump is:

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

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


回答1:


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


来源:https://stackoverflow.com/questions/13414606/casperjs-getelementsbyxpath-only-returning-first-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!