M.Naveed
M.Naveed

Reputation: 1857

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

Upvotes: 1

Views: 610

Answers (1)

Prophet
Prophet

Reputation: 33361

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.

Upvotes: 2

Related Questions