Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath - how to find an embedded li with an input element inside it?

Tags:

xpath

Given this HTML:

<li class="check_boxes input optional" id="activity_roles_input">
    <fieldset class="choices">
        <legend class="label"><label>Roles</label></legend>
        <input id="activity_roles_none" name="activity[role_ids][]" type="hidden" value="" />
            <ol class="choices-group">
                <li class="choice">
                    <label for="activity_role_ids_104">
                        <input id="activity_role_ids_104" name="activity[role_ids][]" type="checkbox" value="104" />Language Therapist
                    </label>
                </li>
                <li class="choice">
                    <label for="activity_role_ids_103">
                        <input id="activity_role_ids_103" name="activity[role_ids][]" type="checkbox" value="103" />Speech Therapist
                    </label>
                </li>
            </ol>
    </fieldset>
</li>

I am trying to use Selenium and xpath with it. I am trying to select the first 'checkbox' input element link.
I am having problems selecting the element.
I cannot use the db ID (104) as this is for repeated tests with new ID's each time. I need to select the 'first' input checkbox, based on it having the text for Language Therapist.

I have tried:

xpath=(//li[contains(@id,'activity_roles_input')])//input

and

xpath=(//li[contains(@id,'activity_roles_input')])//contains('Language Therapist")

but it is not finding the element.

When I do:

xpath=(//li[contains(@id,'activity_roles_input')])

it gets to the input set. The problem I am having is selecting the first input checkbox control for 'Language Therapist'.

like image 641
Michael Durrant Avatar asked Dec 04 '12 20:12

Michael Durrant


1 Answers

First, find any <li> containing the text and than look for in the descendant of those for the first checkbox.

xpath=(//li[contains(., "Language Therapist")]/descendant::input[@type="checkbox"][1])

(From Michael)

The above worked for me. In the end I actually used

xpath=(//li[contains(@id,'activity_roles_input')]/descendant::input[@type="checkbox"][1])

becuase I liked ID'ing by css ID.

like image 51
complex857 Avatar answered Sep 22 '22 23:09

complex857