XPath - 在其他元素之后选择第一个元素 [英] XPath - Select first element after some other element

查看:31
本文介绍了XPath - 在其他元素之后选择第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 XPath 还是很陌生,现在已经玩了几个小时了,所以我不完全确定您是否可以用它做以下类似的事情.

I'm fairly new to XPath, been fiddling around with it for a few hours now, so i'm not entirely sure if you can even do something like the following with it.

好的,场景如下:我想从页面中查找链接.该链接只能通过其文本值识别,即.<a>之间的文本标签(<a href="#">此链接<a>).到目前为止,我已经设法将元素与该文本链接起来,唯一的问题是周围有一些.

Okay, here's the scenario: I want to find a link from a page. That link is only recognizable by it's text value, ie. the text between <a> tags (<a href="#">this link<a>). So far i've managed to get my hands on to link elements with that text, the only problem is that there's a few of those lying around.

这些链接是从前面有另一个链接标签的无序列表中找到的,这将作为一个非常好的锚"点开始搜索我想要的最后一个元素找到(即,我可以只接受第一个匹配的)

These links are found from unordered lists which are preceded by another link tag, which would serve as a really good "anchor" point to begin the search for the final element that i want to find (ie. then i could just accept the first one that matches)

为了澄清一些事情,以下是正在发生的事情的示例:

<a href="#">first dropdown menu</a>
<ul>
  <li><a href="#">some link</a></li>
  <li><a href="#">link i want to find</a></li>
</ul>

<-- *And i would actually want to find the thing from this list* --> 
<a href="#">second dropdown menu</a>
<ul>
  <li><a href="#">some link</a></li>
  <li><a href="#">link i want to find</a></li>
</ul>

而且我可能应该指定,我只想接收一个结果或一组结果,其中第一个元素是正确"元素 - 我想要找到的元素.

And i should probably specify, that i only want to receive either one result or a set of results with the first element being the "correct" element - the element i want to find.

这个问题已经回答了,但有一些评论说我应该多说明一点,这样人们才能真正理解这个问题;)

The question has been answered already, but there were some comments that I should specify this a bit more, so that people could actually understand the question ;)

所以我们的想法是使用一个元素来指定另一个元素的位置,该元素可能会在文档中散布重复的搜索结果.

So the idea was to use an element to specify the location of another element that could have duplicate search results scattered all around the document.

基本上,如果您想从一组具有相同名称或值的元素的下拉菜单中找到给定链接,您会遇到类似的情况.

Essentially you would run into something like this if you wanted to find a given link from a group of dropdown menus that would have elements with same names or values.

基本上就是这样.我知道理解这一点仍然有点困难,但不幸的是,我很难更好地解释它.我相信其他人可以做得更好,如果发生这种情况,我非常乐意在此处包含该描述.

That's basically it. I know that it's still a bit difficult to get the point, but unfortunately I'm having a hard time trying to explain it better. I'm sure that somebody else could do a better job and if that happens, I'm more than happy to include that description here.

推荐答案

我不得不通读你的问题几次,但我想我明白了.您感兴趣的是谓词.谓词允许您根据条件选择节点.

I had to read through your question a couple of times but I think I understand it. What you are interested in is predicates. Predicates allow you to pick nodes based on conditions.

例如,您可以这样做:

//a[text()='second dropdown menu']/following::ul[1]/li/a[text()='link i want to find']

这将选择包含特定文本的任何锚点,找到下一个 ul,然后继续遍历它的子元素.

this would select any anchor with certain text in, find the next ul, then proceed through it's children.

此外,您可以在结果集中指定位置索引,以下 XPath 是一个演示(但它不会解决您的问题):

Also, you can specify positional index within a result set, the following XPath is a demonstration (but it won't solve your problem):

//a[text()='first dropdown menu']/ul/li[last()]/a/text()

或者您可以使用轴在兄弟姐妹/祖先/孩子之间导航:

or you could use axes to navigate across siblings/ancestors/children:

//a[ancestor::ul/preceding::a[1]/text() = 'second dropdown menu']/text()

所以我不确定我是否完全理解您的问题,但这应该可以帮助您编写 XPath.

So I'm not sure I quite understood your question but this should help you write your XPath.

基本上,我假设您的 XPath 与多个列表中的锚点匹配,并且您希望确保选择正确的一个.在 XPath 中的某个时刻,您需要一个谓词来指定一个条件,该条件仅对您所需节点所在的列表为真.

Basically, I'm assuming your XPath matches the anchor in multiple lists and you want to make sure you pick the right one. At some point in your XPath you need a predicate to specify a condition that will only be true for the list your desired node is in.

这篇关于XPath - 在其他元素之后选择第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆