包含一个属性且其父级的父级包含另一个属性的 XPath 元素

发布于 2024-08-20 14:51:28 字数 974 浏览 10 评论 0原文

这是我在这里发表的第一篇文章,因为我看到了很多很棒的答案,所以我想我应该尝试一下。

我正在尝试使用 XPath 来获取 HTML 文档中的特定元素。下面是一个基于 Google 网页的示例:

<html>
  <head>
    <title>XPath Test</title>
  </head>
  <body>
    <form name='f'>
      <table>
        <tbody>
          <tr>
            <td>
              <input name='q' type="text" />
            </td>
          </tr>
        </tbody>
      </table>
    </form>
  </body>
</html>

使用上面的示例(为了寻求帮助而进行了简化),我希望能够找到 name='q' 且具有祖先是 5 个父母,名字='f'。

例如,如果我要使用 SWAT 的语法来执行此操作,SWAT 是一个开源 Web 自动化测试库,位于 http: //ulti-swat.wikispaces.com,语法如下:

|AssertElementExists|Expression|name=q;parentElement.parentElement.parentElement.parentElement.parentElement.name=f|input|

我刚刚开始学习 XPath,并试图了解如何将谓词与轴结合起来。是否可以使用 XPath 执行这样的表达式?如果可以请有了解的人帮忙吗?

This is my first post here and since I've seen many great answers I thought I'd give it a try.

I'm trying to use XPath to obtain a specific element in an HTML document. Here is an example based off of the Google web page:

<html>
  <head>
    <title>XPath Test</title>
  </head>
  <body>
    <form name='f'>
      <table>
        <tbody>
          <tr>
            <td>
              <input name='q' type="text" />
            </td>
          </tr>
        </tbody>
      </table>
    </form>
  </body>
</html>

Using the example above (which is simplified for the purposes of asking for help), I would like to be able to find the input element whose name='q' and who has an ancestor who is 5 parents up with the name='f'.

For example if I were to do this in the syntax of S.W.A.T. which is an open-source web automation testing library located at http://ulti-swat.wikispaces.com, the syntax would be as follows:

|AssertElementExists|Expression|name=q;parentElement.parentElement.parentElement.parentElement.parentElement.name=f|input|

I just started learning XPath and am trying to understand how to combine predicates with axes. Is it possible to do expressions like this with XPath? If so can someone knowledgeable please help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

属性 2024-08-27 14:51:28

您可以执行以下操作:

//input[@name='q' and ancestor-or-self::*[@name='f']]
// -- input element whose name='q' and who has an ancestor with the name='f'

或者

//input[@name='q' and ../../../../../../*[@name='f']]
// -- input element whose name='q' and who is 5 parents up with the name='f'

您可以使用此桌面 XPath Visualizer 来测试您的 XPath 表达式。 可以在此处找到基本教程

You could to do:

//input[@name='q' and ancestor-or-self::*[@name='f']]
// -- input element whose name='q' and who has an ancestor with the name='f'

or

//input[@name='q' and ../../../../../../*[@name='f']]
// -- input element whose name='q' and who is 5 parents up with the name='f'

You can use this desktop XPath Visualizer to test your XPath expressions. A basic tutorial can be found here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文