2012-11-16 7 views
7

Io uso il seguente codice per ottenere tutte le celle della tabella nella prima riga della tabella. Mi piacerebbe quindi controllare l'innerHTML di ogni singola cella di tabella. Ma nell'oggetto restituito da questa funzione solo la prima cella è in realtà lì, tutte le altre proprietà sono nulli:CasperJS getElementsByXPath restituisce solo il primo elemento

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

utils.dump(firstRow); 

L'uscita dal utils.dump è:

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

Ho anche provato con utils .findAll ed era lo stesso. Come posso ottenere tutti gli elementi abbinati?

+0

la prima risposta qui risponde alla mia domanda: http://stackoverflow.com/ domande/10740907/get-all-table-rows-and-return-them-using-an-xpath-query-in-casperjs – leah

+0

Dai un'occhiata a questo, che dovrebbe aiutare: http: // stackoverflow. com/domande/10740907/casperjs ottenendo-all-tavolo-righe-e-ritorno-li-utilizzando-an-xpath-query-in- –

risposta

4

Con Casper/PhantomJS evaluate() funzioni, è necessario mappare elementi DOM nativi e gli elenchi di elementi a qualcosa di JSON-serializzabile:

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