rob waminal
rob waminal

Reputation: 18419

Selenium verify checkbox is checked by text

I got this List of checkboxes and I want to verify if the specific check box is checked or not, I can't use the checkbox id but only the text but it is like this.

<ul id="chkList">
    <li>
        <span>
            <input id="CheckBox_1" type="checkbox" value="1" checked="checked" />
            <label for="CheckBox_1">Cat</label>
        </span>
    </li>
    <li>
        <span>
            <input id="CheckBox_2" type="checkbox" value="20" />
            <label for="CheckBox_2">Dog</label>
        </span>
    </li>
    <li>
        <span>
            <input id="CheckBox_2" type="checkbox" value="22" />
            <label for="CheckBox_2">Alley</label>
        </span>
    </li>
</ul>

I want to do something like

driver.FindElement(By.XPath("//ul[@id='chkList']/li/span/label[./text()='Cat'])).Selected;

But surely I can't do that because the xpath selects the label not the input. I am thinking of using the for for the label to look for the id for input. But how can I do that?

I can't use the id for input and it's value because it is dynamic, the only thing I can use is the Text.

EDIT

Test Case:

  1. Create a new item in chkList
  2. Assign the new item by checking it with another item. (not shown for brevity)
  3. Save
  4. Verify if the new item is assigned to another item.

Upvotes: 1

Views: 4136

Answers (1)

Dhivya
Dhivya

Reputation: 699

Have you tried with XPath like this:

driver.FindElement(By.XPath("//ul[@id='chkList']/li[1]/span/input)).Selected;

You can maintain one count for number of checklist and use that value with li[count] element.

Upvotes: 0

Related Questions