在页面上下文中使用 CasperJS 的 getElementInfo

Using CasperJS' getElementInfo in the page context

数据

<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

但是我只想从元素 EM 中获取 'Platform 1',我该怎么做,我已经尝试使用 getElementInfo 和 FetchText - 请参阅我已注释掉的代码,但都不起作用。有什么想法吗?

改变

return e.innerText;

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

这是Element API的一个基本例子。您不能使用 casper.getElementInfo,因为 casper 未在页面上下文内部定义(在 casper.evaluate() 内部)。

您需要确保 e.querySelector("em") 不为空。