2016-03-24 101 views
0

數據在頁面上下文使用CasperJS」 getElementInfo

<a href="/route" data-eventid="train_card" data-eventlabel="2:10"> <strong>2:10</strong> <h5>Bristol to London</h5> <em>Platform 1</em> </a> 

代碼

function getPlatform() { 
    var types = document.querySelectorAll('.appList a'); 
    return Array.prototype.map.call(platforms, function(e){ 
     return e.innerText; 
    // return e.getElementInfo('em'); 
    // return e.fetchText('a em'); 
    }); 
}; 

如果我使用的innerText我得到: -

2:10 
Bristol to London 
Platform 1 

不過,我只希望從此獲得'平臺1' e元素EM,我怎麼能這樣做,我已經嘗試使用getElementInfo & FetchText - 看到我已經註釋掉的代碼,既沒有作品..任何想法?

回答

0

變化

return e.innerText; 

return e.querySelector("em").textContent; 

這是Element API的基本示例。您不能使用casper.getElementInfo,因爲casper未在頁面上下文內部定義(在casper.evaluate()之內)。您需要確保e.querySelector("em")不爲空。

+0

謝謝,這工作..我只是學習..你能解釋你的意思是你的'casper沒有定義'的聲明。 ?並重新'..是空的,我假設你的意思是測試,以確保結果之前有一些東西返回它。 – user2596590

+0

'e'是一個DOM元素,它沒有'getElementInfo'函數。有'casper.getElementInfo'函數,但在頁面上下文中不可用。 [頁面上下文是沙箱](http://phantomjs.org/api/webpage/method/evaluate.html),所以你不能使用外部定義的變量,也不可能將複雜的對象傳遞到頁面上下文中。 –

+0

感謝您的幫助 – user2596590