Home > Software design >  How I can find a checkbox element associated with a label
How I can find a checkbox element associated with a label

Time:01-06

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"  
  checked="">
  <span ><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

CodePudding user response:

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.

  •  Tags:  
  • Related