【发布时间】:2015-10-09 14:01:45
【问题描述】:
我在一列中有一个复选框列表。我正在尝试从第二行中选择复选框。我尝试了以下 XPATH,但它选择了所有复选框。
//table[@id="match_configuration_add_possible_tab_match_rules_tb_match_rules"]//input[@type = "checkbox"]
HTML 是:
<table id="match_configuration_add_possible_tab_match_rules_tb_match_rules" class="GOFU2OVJE border" cellspacing="0" __gwtcellbasedwidgetimpldispatchingfocus="true" __gwtcellbasedwidgetimpldispatchingblur="true">
<thead aria-hidden="false">
<tr __gwt_header_row="0">
<th class="GOFU2OVID GOFU2OVGD" __gwt_header="header-gwt-uid-311" __gwt_column="column-gwt-uid-310" colspan="1">
<span style="">
<input type="checkbox"/>
</span>
</th>
<th class="GOFU2OVID GOFU2OVAE" __gwt_header="header-gwt-uid-313" __gwt_column="column-gwt-uid-312" colspan="1">Name</th>
</tr>
</thead>
<colgroup>
<tbody style="">
<tr class="GOFU2OVCD GOFU2OVMD" __gwt_subrow="0" __gwt_row="0">
<td class="GOFU2OVBD GOFU2OVDD GOFU2OVED GOFU2OVND">
<div __gwt_cell="cell-gwt-uid-299" style="outline-style:none;">
<input type="checkbox" tabindex="-1"/>
</div>
</td>
<td class="GOFU2OVBD GOFU2OVDD GOFU2OVOD GOFU2OVLD GOFU2OVND">
<div __gwt_cell="cell-gwt-uid-300" style="outline-style:none;">
<input id="" class="" type="text" style="color: blue;" value=""/>
</div>
</td>
</tr>
<tr class="GOFU2OVCE GOFU2OVJD" __gwt_subrow="0" __gwt_row="1">
<td class="GOFU2OVBD GOFU2OVDE GOFU2OVED GOFU2OVKD">
<div __gwt_cell="cell-gwt-uid-299" style="outline-style:none;">
<input type="checkbox" tabindex="-1"/>
</div>
</td>
<td class="GOFU2OVBD GOFU2OVDE GOFU2OVOD GOFU2OVKD">
</tr>
</tbody>
<tbody style="display: none;">
<tfoot style="display: none;" aria-hidden="true"/>
</table>
我想选中具有 cell-gwt-uid-299 的复选框 我不能在 XPATH 中使用 UID-299,因为这个值是动态的。当您访问该页面时,数字会发生变化。
我也尝试过使用祖先的以下 XPATH:
//table[@id="match_configuration_add_possible_tab_match_rules_tb_match_rules"]/ancestor::tr[1]//input[@type = "checkbox"]
我已经尝试过使用previous。在开发者工具窗口中,一个复选框被突出显示,但它来自不同的表:
//table[@id="match_configuration_add_possible_tab_match_rules_tb_match_rules"]/preceding::tr[1]//input[@type = "checkbox"]
奇怪的在它前面使用它试图从不同的表中选择一个复选框。这是我使用之前在开发工具中突出显示的 HTML:
<table id="data_configuration_feeds_ct_fields_body" cellspacing="0" style="table-layout: fixed; width: 100%;">
<colgroup>
<tbody>
<tr class="GOFU2OVFG" __gwt_subrow="0" __gwt_row="0">
<tr class="GOFU2OVEH" __gwt_subrow="0" __gwt_row="1">
<tr class="GOFU2OVFG" __gwt_subrow="0" __gwt_row="2">
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVHG">
<div __gwt_cell="cell-gwt-uid-111" style="outline-style:none;">
<input type="checkbox" tabindex="-1"/>
</div>
</td>
我可以使用什么 XPATH?
【问题讨论】:
-
我更喜欢 css 而不是 xpath。方法如下 -
(By.cssSelector(".GOFU2OVJE tbody tr input[type='checkbox']")) -
这个选择了两个复选框,在开发者工具中我选择 CSS 并输入 ".GOFU2OVJE tbody tr input[type='checkbox']"
-
已更新 -
(By.cssSelector(".GOFU2OVJE tbody tr input[type='checkbox']:nth-of-type(1)")) -
当您发布 HTML 时,请花一点时间使用像 jsbeautifier.org 这样的美化工具来正确格式化它。它使阅读变得更容易,从而使您的问题更有可能得到回答。谢谢!
-
@JeffC 我不知道 jsbeautifier.org。从现在开始我将使用它。谢谢。
标签: css selenium xpath selenium-webdriver