Ben
Ben

Reputation: 35

Selenium IDE / Xpath verifying a checkbox is checked only if the element's label contains specific text

I am looking for some guidance on how to write an Xpath query to verify that a specific checkbox is checked (the divs nested in the panel-body div are checkboxes). What I tried to do is make sure that a checkbox div with the corresponding label containing the text "Evaluations" contains the class "checked". Here is my HTML:

<div class="panel-body">
    <div class="checkradios-checkbox checkradios access icon-checkradios-checkmark checked">
        <input id="access_conferences" class="checkradios access" name="access_conferences" value="true" checked="" type="checkbox">
    </div>

    <label for="access_conferences">Conferences</label>
    <br>

    <div class="checkradios-checkbox checkradios access icon-checkradios-checkmark checked">
        <input id="access_evaluations_viewing" class="checkradios access" name="access_evaluations_viewing" value="true" checked="" type="checkbox">
    </div>

    <label for="access_evaluations_viewing">Evaluations - Viewing</label>   
    <br>
</div>

And here is my Xpath: //label[@for="access_evaluations_viewing"]/preceding::div[@class="checkradios-checkbox checkradios access icon-checkradios-checkmark checked"]

The problem is my test case is passing even when that box is unchecked, which tells me my xpath is grabbing other elements. Some of the other checkboxes in the panel-body div are also going to be checked, so what I think may be happening is my Xpath is checking for the next preceding div with a class of "checked", regardless of the text that div's label contains.

Edit: as was suggested, I have tried referencing the div preceding the label. The problem with doing this is that not every box in the panel-body is going to necessarily be checked. I only need to confirm that those associated with "Evaluations" are checked. Referencing the div above the label fails when one or more of the boxes other than "Evaluations" is unchecked. This is my revised Xpath:

//label[@for="access_evaluations_viewing"]/preceding-sibling::div[contains(@class, "unchecked")]

I switched to doing the negative comparison because contains(@class, "checked") will pass for both checked and unchecked boxes.

Upvotes: 0

Views: 687

Answers (2)

Viet Pham
Viet Pham

Reputation: 214

These 4 elements are siblings, so when you at the Label, you must go to the Div above to get the checkbox element.

Try this XPath:

//div[@class='panel-body']/label[contains(.,'Evaluations')]/preceding-sibling::div[1]

Upvotes: 0

Andersson
Andersson

Reputation: 52665

If you need to match label that contains "Evaluations" text you may try

//label[@for="access_evaluations_viewing"][contains(text(), "Evaluations")]/preceding::div[@class="checkradios-checkbox checkradios access icon-checkradios-checkmark checked"]

Upvotes: 0

Related Questions