Testcomplete click on Checkbox using Xpath

1k Views Asked by At

I do have the following html text:

<tr class="off" onmouseout="this.className='off'"onmouseover="this.className='over'">
<td><input type="checkbox" name="caller_enrollment_form:enrollmentParticipantSelectionTable:0:personSelecti‌​on" id="caller_enrollment_form:enrollmentParticipantSelectionTable:0:personSelection‌​" value="true" />
</td>
<td><span id="caller_enrollment_form:enrollmentParticipantSelectionTable:0:extClientId">11‌​1</span>
</td>
<td><span id="caller_enrollment_form:enrollmentParticipantSelectionTable:0:clientName">SAM‌​UEL</span>
</td>

I am trying to click on the checkbox which has the data 111 in the same row.

I am trying to something like this:

page.FindChildByXPath("//span[contains(text(),'111')]/parent::td/preceding-sibling::td/input[@name='caller_enrollment_form:enrollmentParticipantSelectionTable:0:personSelection']",false)

but I get object not found error.

1

There are 1 best solutions below

4
On

Your checkox has an ID, and IDs are unique within a page. So you can use:

// JScript
var checkBox = page.FindChildByXPath("//input[@id='caller_enrollment_form:enrollmentParticipantSelectionTable:0:personSelection‌​']");
checkBox.ClickChecked(true); // Check the checkbox

The ID contains a number (0); you can use a variable to specify this number:

var checkBox = page.FindChildByXPath("//input[@id='caller_enrollment_form:enrollmentParticipantSelectionTable:" + index + ":personSelection‌​']");


If you specifically need to identify by the text "111", you can use something like this:

var checkBox = page.FindChildByXPath("//tr[td/span[.='111']]/td/input");
checkBox.ClickChecked(true); // Check the checkbox

You can also use a variable instead of "111":

var checkBox = page.FindChildByXPath("//tr[td/span[.='" + text + "']]/td/input");

XPath explanation:

//tr                   - find a table row
    [                  - that matches this condition:
      td/span[.='111'] - any of the row's cells contains a SPAN with the text '111'
    ]
                       - then in this row
 /td/input             - find an INPUT in any cell