Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CasperJS getElementsByXPath only returning first element

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?

like image 251
leah Avatar asked Nov 16 '12 10:11

leah


1 Answers

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);
like image 87
NiKo Avatar answered Nov 11 '22 06:11

NiKo