XPATH:如何选择祖父母的第3个子元素

时间:2015-03-09 01:35:02

标签: selenium xpath

这是我的DOM示例:

<div id="x-auto-2121" class="GI05A-4CEKC">
    <div class="GI05A-4CBKC GI05A-4COJC GI05A-4CGKC GI05A-4CFKC">
        <img style="height: 18px; width: 17px;" src="https://someserver.com/clear.gif"/>
        <img class="GI05A-4CDKC"/>
        <img class="GI05A-4CMJC"/>
        <img class="GI05A-4CCKC"/>
        <span class="GI05A-4CHKC">
            <span>The Unique Value I Can Search For</span>
        </span>
    </div>
</div>

我知道价值&#34;我可以搜索的独特价值&#34;并且可以使用类似//span[text()='The Unique Value I Can Search For']

的内容找到它

问题是我想选择祖父母的第三个孩子:

 <img class="GI05A-4CMJC"/>

我试图使用Selenium来测试非常自动化的UI(诅咒你,GXT)。显然,我对xpath很新。

2 个答案:

答案 0 :(得分:1)

<div id="x-auto-2121" class="GI05A-4CEKC">
    <div class="GI05A-4CBKC GI05A-4COJC GI05A-4CGKC GI05A-4CFKC">  <-- Common ancestor
        <img src="https://someserver.com/clear.gif"/>
        <img class="GI05A-4CDKC"/>
        <img class="GI05A-4CMJC"/>                                 <-- Node of interest
        <img class="GI05A-4CCKC"/>
        <span class="GI05A-4CHKC">
            <span>The Unique Value I Can Search For</span>         <-- Node of interest
        </span>
    </div>
</div>

接近一个

从关键节点开始,向上导航到感兴趣节点的共同祖先,然后深入查看结果。我们的核心是

//span/../../img

然后是指定我们想要哪个spanimg的问题。

//span[text()='The Unique Value I Can Search For']/../../img[3]

接近两个

从感兴趣的节点的共同祖先开始,然后深入研究结果。我们的核心是

//div/img

然后是指定我们想要哪个divimg的问题。

//div[span/span/text()='The Unique Value I Can Search For']/img[3]

答案 1 :(得分:0)

您可以使用具有正确ID的标记启动xpath。如果id不是动态的,您可以使用下面的xpath来获取所需的元素。

//div[@id='x-auto-2121']/div/img[3]

相关问题