niemeczek
niemeczek

Reputation: 35

Xpath How to get one element using another element text value

I am trying to create a xpath for my automated test. I have following code:

<span class="left-floated">
   <input class="PfcExecutiveBrief" type="checkbox" value="PfcExecutiveBrief" name="DocumentTypeResearch"/>
   <label for="PfcExecutiveBrief">Executive Brief</label>
  <br/>
   <input class="Memo" type="checkbox" value="PfcMemo" name="DocumentTypeResearch"/>
   <label for="PfcMemo">Memo</label>
  <br/>
   <input class="PfcOtherResearchForNaturalGasOrOil" type="checkbox" checked="checked" value="PfcOtherResearchForNaturalGasOrOil" name="DocumentTypeResearch" disabled=""/>
   <label for="PfcOtherResearchForNaturalGasOrOil">Other Research</label>
  <br/>
   <input class="PfcProfile" type="checkbox" value="PfcProfile" name="DocumentTypeResearch"/>
   <input type="hidden" value="PfcOtherResearchForNaturalGasOrOil" name="DocumentTypeResearch"/>
   <label for="PfcProfile">Profile</label>
  <br/>
</span>

I want to create xpath which help me to check input element for selected label. So my question is, how can I get single input element for each label?

For example if:

label[text()='Memo']

how to get second input etc, etc..

Upvotes: 1

Views: 466

Answers (1)

Jens Erat
Jens Erat

Reputation: 38662

Although the @for attribute is misused here (it should point to the @id attribute), you can use it to resolve to the matching input that uses the same @value attribute.

//input[@value=../label[text()='Memo']/@for]

If the label might be everywhere in the document (instead of being a sibling like in your example), you can also search from the root again:

//input[@value=//label[text()='Memo']/@for]

Upvotes: 2

Related Questions