Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How I can find a checkbox element associated with a label

Im trying to create a xpath of a checkbox which has only label as unique identifier. The given Id or values change when a new checkbox element is created.

<li>
  <input type="checkbox" value="f052503c-28c2-4b2d-8bd3-0ef6cc0e563a" id="f052503c-28c2-4b2d- 
  8bd3-0ef6cc0e563a" name="ElementIds" class="i-checks" 
  checked="">
  <span class="m-l-xs"><label for="f052503c-28c2-4b2d-8bd3-0ef6cc0e563a">AuElement</label> 
  </span>
</li>

There are a couple of options i have tried so far but nothing is working:

CheckboxElement = //input[@type = 'checkbox' and @label = 'AuElement']
//input[@type='checkbox']//span[@label='AuElement']

For now I tried to use:
//*[contains(text(), 'AuElement')] xpath but i cannot use
if CheckboxElement.Selected() method to check if checkbox is already selected or not. Any help would be highly appreciated. Thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use this XPath:

"//li[.//label[text()='AuElement']]//input"

Explanation:
Find such li element that contains inside label element with text value of "AuElement". Now find input (checkbox) element inside this li element (it’s child).
In other words:
You can use common parent element so that this parent element is found based on it’s unique child label and then go to the input element inside it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading