2010-02-02 57 views
6

这是我在这里的第一篇文章,因为我看到很多很好的答案,我想我会试试看。包含一个属性的XPath元素,其父项的父项包含另一个属性

我正在尝试使用XPath获取HTML文档中的特定元素。这是基于关闭谷歌网页的例子:

<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> 

使用上面的例子(这是简化了寻求帮助的目的),我希望能够找到输入元素,其名称= 'q',并且谁的祖先是5个父母,名字为'f'。

例如,如果我要在S.W.A.T的语法中这样做。这是位于http://ulti-swat.wikispaces.com一种开源网络自动化测试库,语法如下:

| AssertElementExists |表达式|名= Q; parentElement.parentElement.parentElement.parentElement.parentElement.name = F |输入|

我刚开始学习XPath,并试图理解如何将谓词与轴相结合。是否有可能使用XPath执行此类表达式?如果有的话,有人可以提供帮助吗?

回答

13

,你可以这样做:

//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表达式。 A basic tutorial can be found here

相关问题