【问题标题】:Testcomplete click on Checkbox using Xpath使用 Xpath 测试完成单击复选框
【发布时间】:2015-06-15 21:35:22
【问题描述】:

我确实有以下 html 文本:

<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>

我正在尝试单击同一行中包含数据 111 的复选框。

我正在尝试这样的事情:

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

但我得到 object not found 错误。

【问题讨论】:

  • 欢迎来到 StackOverflow。我已将您的 HTML 合并到问题中,并添加了一些更合适的标签。您可以像我一样单击edit 按钮添加内容以改进问题中的细节。希望你能找到答案。

标签: html xpath testcomplete


【解决方案1】:

您的复选框有一个 ID,并且 ID 在一个页面中是唯一的。所以你可以使用:

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

ID 包含一个数字 (0);你可以使用一个变量来指定这个数字:

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


如果你特别需要通过文本“111”来识别,你可以使用这样的:

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

你也可以用变量代替“111”:

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

XPath 解释:

//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

【讨论】:

  • 谢谢海伦...我希望我可以使用第一个答案,但是 ID 中的数字 0 不断变化...这是行号.. 有没有办法可以在 th 中附加行索引我会字段 - 类似'caller...'+RowIndex+':personSelection'??我会尝试第二个选项,看看。感谢您的帮助
  • 我也尝试了第二个选项。在这里,每次运行我的跨度文本也会更改,下次将是 112。那么如何在 FindChildByXPath 函数中插入动态值呢?
  • 谢谢 Helen ..我得到了它的使用:FindChildByXPath("//tr[td/span[.='"+CIN+"']]/td/input");你能解释一下它的作用吗?
  • @test:很高兴为您提供帮助!我在答案中添加了 XPath 解释。
  • 非常感谢您的精彩解释!
猜你喜欢
  • 2020-06-02
  • 1970-01-01
  • 2013-08-06
  • 1970-01-01
  • 2020-10-06
  • 2019-06-18
  • 1970-01-01
  • 2021-08-20
  • 1970-01-01
相关资源
最近更新 更多