如何从使用角度写的网页中使用xpath提取文本?

时间:2017-03-11 04:23:15

标签: javascript angularjs xpath google-voice

以前,谷歌语音允许你使用“div”提取短信代码最近,他们使用AngularJS切换到基于ARIA的框架。如何从网页中提取包含我所有短信代码的字符串?

这是我一直在使用的目标xpath命令:

var search2 = document.evaluate('//*[@id="messaging-view"]/div/md-content/div/gv-conversation-list/md-virtual-repeat-container/div/div[2]/div[1]/div/gv-text-conversation-item/gv-conversation-item/div/div[2]/ng-transclude/gv-conversation-item-detail/gv-annotation/text()',
  document, null, XPathResult.ANY_TYPE, null);

Here is the result I see on my Chrome Developer console

1 个答案:

答案 0 :(得分:0)

看看Mozilla documentation on XPath in javascript。你得到的东西叫做节点迭代器。如果您像指定ANY_TYPE那样,这可能是XPath查询最常见的返回类型。要使代码按原样运行,您可以执行以下操作:

// im putting your query in a variable for readability
var query = '//*[@id="messaging-view"]/div/md-content/div/gv-conversation-list/md-virtual-repeat-container/div/div[2]/div[1]/div/gv-text-conversation-item/gv-conversation-item/div/div[2]/ng-transclude/gv-conversation-item-detail/gv-annotation/text()';
var search2 = document.evaluate(query, document, null, XPathResult.ANY_TYPE, null);

var thisNode = iterator.iterateNext();

while (thisNode) {
    console.log(thisNode.textContent); // probably will be a text node
    thisNode = iterator.iterateNext();
}   

或者,您可以将结果类型指定为STRING_TYPE

// im putting your query in a variable for readability
var query = '//*[@id="messaging-view"]/div/md-content/div/gv-conversation-list/md-virtual-repeat-container/div/div[2]/div[1]/div/gv-text-conversation-item/gv-conversation-item/div/div[2]/ng-transclude/gv-conversation-item-detail/gv-annotation/text()';
var search2 = document.evaluate(query, document, null, XPathResult.STRING_TYPE, null);

console.log(search2.stringValue);