简体   繁体   English

如何查找/获取文本节点的前一个同级节点?

[英]How to find/get the preceding-sibling node of a text node?

So I'm trying to get the span node in the html below using xpath, and the only things I want to be used in the xpath are the text node "follower" and the span node itself, not any other nodes. 因此,我尝试使用xpath在下面的html中获取span节点,而我想在xpath中使用的唯一内容是文本节点“ follower”和span节点本身,而不是其他任何节点。

 <li class="-nal3 "> <span class="g47SY ">1</span> follower </li> 

In other words, all the information we have is that, there is a text node in our html indicating "follower" and its preceding sibling is a span node. 换句话说,我们所拥有的所有信息是,我们的html中有一个文本节点,指示“跟随者”,其前面的同级是一个span节点。 That's all we know. 我们知道的就这些。 and we want to get an attribute of that span node. 我们想要获取该span节点的属性。

I tried a lot of things but none of them seems to work! 我尝试了很多东西,但似乎都没有用! I searched a lot but couldn't find anything on how to use those text nodes in xpath, let aside finding other nodes using these nodes haha. 我进行了很多搜索,但找不到关于如何在xpath中使用这些文本节点的任何信息,而撇开使用这些节点查找其他节点的哈哈。 Would appreciate if anyone can help. 如果有人可以帮助,将不胜感激。 (I'm actually using Rselenium to to the job btw) (实际上我正在使用Rselenium来完成工作)

This will return you the <span> node: 这将返回<span>节点:

//text()[contains(., 'follower')]/preceding-sibling::span

And this too: 这也是:

//*[text()[contains(., 'follower')]]/span

Or this one: 或者这个:

//span[contains(following-sibling::text(), 'follower')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM