Reputation: 171
I have division with class ="viewSaveDietData" that contains spans with same class name. I am not able to differentiate the XPATHS of the different spans. Please tell me the XPATH of the individual spans..
<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>
I want to get the xpath of "Balanced"
Upvotes: 1
Views: 6041
Reputation: 62
Got something similar to Larsh, but using /*/ instead of // as this makes search quicker. If titles are unique and if you are interested in answer for Diet Category the below code or one from LarsH should be good.
/*/div[@class="viewSaveDietTitle" and text()="Diet Category:"]/following-sibling::div[1]/span/text()
Upvotes: 0
Reputation: 96484
Some ways that I just tested (via the firefox selenium plugin and also the firefox xpath plugin) are:
//div[@class="viewSaveDietData"]/span[contains(text(), "Balanced")]
Also (more specificly):
//div[@class="viewSaveDietData"]/span[contains(@class, 'Answer') and text()="Balanced"]
Just by position with:
//div[@class="viewSaveDietData"][4]/span
By Answer span alone (doesn't depend on being in the div) with
//span[contains(@class, 'Answer') and text() = "Balanced"]
(or more simply)
//span[@class="Answer" and text()="Balanced"]
So:
//div[@class="viewSaveDietData"]/span[contains(@class, 'Answer') and text()="Balanced"]
is most specific which of course means it's most likely to be specific in selecting the desired elements but also will be more affected by page changes.
Upvotes: 0
Reputation: 27995
I'm not sure I understood your question, but it sounds like you're looking for something like
//div[@class='viewSaveDietTitle'][. = 'Diet Category']/
following-sibling::div[1][@class='viewSaveDietData']/span/text()
This will select the text of the span that's a child of the div whose class is viewSaveDietData
, which immediately follows a div containing the title string 'Diet Category'. In other words, my understanding of your question is, how to select a particular span based on the title of the associated div
.
If you just want to select a span based on its order in the document, you could use
(//span[@class = 'Answer'])[4]
Upvotes: 1