1

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..

1 Answer 1

2

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.