2016-01-03 32 views
0

我从网站上刮取足球比分数据。所有比分都在一张桌子上,每个<tr>都有“封锁主场比赛17”和一些独特的东西。CasperJS'getElementsByXpath返回null与有效的XPath

我在Chrome开发工具中测试了我的xpath,它只识别我需要的表行。

var utils = require('utils'); 
var casper = require('casper').create(); 
var xpath = require('casper').selectXPath; 
var result = []; 

function getScores(){ 
    console.log("getting scores"); 
    result = __utils__.getElementsByXPath("//tr[contains(@id,'block_home_matches_17')"); 
} 

casper.start('http://int.soccerway.com/', function() { 
    console.log("casper start....");  
    var l = getScores(); 
    utils.dump(l); 
}); 

casper.run(); 

该代码返回[] as utils.dump!为什么?我的xpath是有效的!

+0

有关? http://stackoverflow.com/questions/3655549/xpath-containstext-some-string-doesnt-work-when-used-with-node-with-more –

回答

4

你有三个问题:

可以检索表示您的目标DOM的节点要么通过CasperJS功能:

casper.start('http://int.soccerway.com/', function() { 
    utils.dump(this.getElementsInfo(xpath("//tr[contains(@id,'block_home_matches_17')"))); 
}); 

或通过在页面上下文中的元素直接工作:

casper.start('http://int.soccerway.com/', function() { 
    utils.dump(this.evaluate(function(){ 
     return __utils__.getElementsByXPath("//tr[contains(@id,'block_home_matches_17')").map(function(el){ 
      return {} // TODO: produce your own representation 
     }); 
    })); 
}); 
+0

此外,你不会从'getScores()'返回任何东西'所以'l'将始终为空。 –