xpath / css子类的选择器

时间:2014-07-21 22:27:07

标签: xpath

我有一个HTML文档,其结构如下:

<div class="parent">
  <!--more stuff here-->
    <div class="child">
    </div>
</div>

我想选择位于类'child'下面的'parent'类的元素。下面是否可以在xpath中完成工作,和/或有更好的方法吗? (请注意,任何数量的元素都可以位于上面的<div>之间。)

//div[@class='parent']//div[@class='child']

1 个答案:

答案 0 :(得分:1)

如果您打算在child内选择任何深度为parent的所有元素,例如:

<div class="parent">
  <!--more stuff here-->
    <div class="child">
    </div>
    <div class="other">
        <div class="child">
        </div>
    </div>
</div>

..然后是,您当前的XPath将会执行,上面示例中的child都将被选中。或者,如果您只想要父母的直接孩子,您可以使用单斜杠:

//div[@class='parent']/div[@class='child']