Html XPath 检索跨度内的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16241197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:54:44  来源:igfitidea点击:

XPath to retrieve text within span

htmlxpathxpath-2.0

提问by shivamsupr

I am trying to figure out the XPath which will retrieve the 'text Data 3' from the following HTML snippet

我试图找出将从以下 HTML 片段中检索“文本数据 3”的 XPath

  <span class="inner-span">
    Text-data 1
    <br>
    <span>Text Data 2</span> text Data 3
   </span>

So far i have tried the following Xpath which leads me to the span with class 'inner-span'

到目前为止,我已经尝试了以下 Xpath,它使我进入了“inner-span”类的跨度

 /html/body/div/div[4]/div[2]/div[2]/div/div[2]/div/span[@class="inner-span"]

But dont know what to add more to Xpath which will only give me text 'text Data 3' from above Html snippet. Thanks

但不知道要向 Xpath 添加更多内容,它只会从上面的 Html 片段中给我文本“文本数据 3”。谢谢

回答by Rolando Isidoro

For your particular case this XPath query would work:

对于您的特定情况,此 XPath 查询将起作用:

normalize-space(//span[@class="inner-span"]/text()[last()])

Tried it in this online testerand here's the result:

在这个在线测试器中尝试过,结果如下:

  1. Your HTML;
  2. My XPath expression;
  3. The expected result "text Data 3".
  1. 你的 HTML;
  2. 我的 XPath 表达式;
  3. 预期结果“文本数据 3”。

enter image description here

在此处输入图片说明

回答by Martin Honnen

You could select /html/body/div/div[4]/div[2]/div[2]/div/div[2]/div/span[@class="inner-span"]/text()[last()]to get the text node (though with leading and trailing white space) or you can use an XPath returning a string with normalize-space(/html/body/div/div[4]/div[2]/div[2]/div/div[2]/div/span[@class="inner-span"]/text()[last()]).

您可以选择/html/body/div/div[4]/div[2]/div[2]/div/div[2]/div/span[@class="inner-span"]/text()[last()]获取文本节点(尽管带有前导和尾随空格),或者您可以使用 XPath 返回带有normalize-space(/html/body/div/div[4]/div[2]/div[2]/div/div[2]/div/span[@class="inner-span"]/text()[last()]).