我如何通过内部文本选择HTML元素?

时间:2018-09-20 08:41:26

标签: html xpath

例如,我具有这样的结构:

<div>
  GLaDOS 
  <div>
    is a fictional artificially intelligent computer system 
  </div>
  from the video game series 
  <div>
    Portal
  </div>
</div>

<div>
  from the video game series 
  <div>
    is a fictional artificially intelligent computer system 
  </div>
  123 
  <div>
    Portal
  </div>
</div>

我想选择在第一和第二内部div之间包含“来自视频游戏系列”的元素。 xpath表达式应如何显示?

2 个答案:

答案 0 :(得分:1)

要从视频游戏系列中选择包含文本 div元素,可以使用XPath表达式//div[contains(., 'from the video game series')]"

要将选择内容限制为第一个div和第二个//div[contains(text()[count(preceding-sibling::div) = 1 and count(following-sibling::div) > 0], 'from the video game series')]子之间的文本,请尝试以下操作:final String rootPath = Environment.getExternalStorageDirectory().getPath() + "/Myapp/music/"; String p1 = rootPath + "outputfiles/1.wav"; String p2 = rootPath + "outputfiles/com.wav"; Wave w1 = new Wave(p1); Wave w2 = new Wave(p2); FingerprintSimilarity fingerprintSimilarity = w1.getFingerprintSimilarity(w2); float score = fingerprintSimilarity.getScore(); float similarity = fingerprintSimilarity.getSimilarity(); Log.d("Similar sound :", "Score : " + score + "\n Similarity : "+ similarity);

答案 1 :(得分:0)

此解决方案与来源一样可靠(例如,如果来源发生变化,它将被破坏):

//text()[contains(., 'from the video game series')][preceding-sibling::div and following-sibling::div]

您可以here对其进行测试。