如何在多个跨度中为具有相同类的跨度定义XPATH?

时间:2015-06-11 16:59:24

标签: html xpath

我与class =" viewSaveDietData"包含具有相同类名的跨度。我无法区分不同跨度的XPATHS。请告诉我个人跨度的XPATH ..

<div class="viewSaveDietTitle" >Created By:</div><div class="viewSaveDietData" ><span class="Answer"><b style='color:#386AA1;'><a href="/www/community/ProfileWall.aspx?f=FitClick" title="FitClick">FitClick</a></b> on Jan 27, 2012</span></div>
                    <div style="clear:both;"></div>

                    <div class="viewSaveDietTitle" >Users:</div><div class="viewSaveDietData" ><span class="Answer">3,403 (<a href="/online_weight_loss_support?cdID=23982">Find someone using this diet</a>)</span></div>
                    <div style="clear:both;"></div>

                    <div class="viewSaveDietTitle" >Comments:</div><div class="viewSaveDietData" ><span class="Answer"><a href="#jump-to-comments">Be the first!</a></span></div>
                    <div style="clear:both;"></div>

                    <div class="viewSaveDietTitle" >Diet Category:</div><div class="viewSaveDietData" ><span class="Answer">Balanced</span></div>
                    <div style="clear:both;"></div>

                    <div class="viewSaveDietTitle" >Diet Plan Length:</div><div class="viewSaveDietData" ><span class="Answer">Ongoing</span></div>
                    <div style="clear:both;"></div>

                    <div class="viewSaveDietTitle" >Meals Per Day:</div><div class="viewSaveDietData" ><span class="Answer">3 meals, 4 meals, 5 meals, 6 meals</span></div>
                    <div style="clear:both;"></div>

                    <div class="viewSaveDietTitle" >Target Gender:</div><div class="viewSaveDietData" ><span class="Answer">Women and men</span></div>
                    <div style="clear:both;"></div>

                    <div class="viewSaveDietTitle" >Weight Goal:</div><div class="viewSaveDietData" ><span class="Answer">Lose weight, Maintain weight, Gain weight</span></div>
                    <div style="clear:both;"></div>

                    <div class="viewSaveDietTitle" >Cooking Difficulty:</div><div class="viewSaveDietData" ><span class="Answer">Easy</span></div>
                    <div style="clear:both;"></div>

                    <div class="viewSaveDietTitle" >Tags:</div><div class="viewSaveDietData" ><span class="Answer">52.5:22.5:25 Macronutrient Ratio</span></div>
                    <div style="clear:both;"></div>

我想获得&#34;平衡&#34;

的xpath

3 个答案:

答案 0 :(得分:0)

我不确定我理解你的问题,但听起来你正在寻找像

这样的东西
//div[@class='viewSaveDietTitle'][. = 'Diet Category']/
     following-sibling::div[1][@class='viewSaveDietData']/span/text()

这将选择范围为viewSaveDietData的div的子级的跨度文本,该子句紧跟在包含标题字符串&#39; Diet Category&#39;的div之后。换句话说,我对您的问题的理解是,如何根据相关div的标题选择特定范围。

如果您只想根据文档中的顺序选择范围,可以使用

(//span[@class = 'Answer'])[4]

答案 1 :(得分:0)

我刚测试的一些方法(通过firefox selenium插件以及firefox xpath插件)是:

//div[@class="viewSaveDietData"]/span[contains(text(), "Balanced")]

(更具体地说):

//div[@class="viewSaveDietData"]/span[contains(@class, 'Answer') and text()="Balanced"]

只是位置:

//div[@class="viewSaveDietData"][4]/span

通过单独的答案范围(不依赖于在div中)和

//span[contains(@class, 'Answer') and text() = "Balanced"]

(或更简单地说)

//span[@class="Answer" and text()="Balanced"]

所以:

//div[@class="viewSaveDietData"]/span[contains(@class, 'Answer') and text()="Balanced"]

是最具体的,当然这意味着它最有可能在选择所需的元素时具体,但也会受到页面更改的影响。

答案 2 :(得分:0)

类似于Larsh,但使用/ * /而不是//因为这会使搜索更快。如果标题是唯一的,如果您有兴趣回答饮食类别,则以下代码或LarsH的代码应该是好的。

/*/div[@class="viewSaveDietTitle" and text()="Diet Category:"]/following-sibling::div[1]/span/text()