如何根据行值选择表中的复选框?

时间:2016-03-02 20:23:44

标签: python selenium

我正在努力实现GWT应用的自动化。有一个包含以下列的表:checkbox,userID,Username,Fname,LName,Email。

以下是表格中单行的代码:                                                                 

</div>
</td>
</tr>
</tbody>
</table>
</div>
</td>
<td class="GB1F3LJFQ GB1F3LJKO" title="8902" align="right" style="height: 19px; width: 34px;">
<div style="height: 19px; width: 34px;">
<div class="gwt-HTML">8902</div>
</div>
</td>
<td class="GB1F3LJFQ GB1F3LJKO" title="Agency_Group_0_Agency_Group_Member_0_dan_all_features_6" align="left" style="height: 19px; width: 146px;">
<div style="height: 19px; width: 146px;">
<div class="gwt-HTML">Agency_Group_0_Agency_Group_Member_0_dan_all_features_6</div>
</div>
</td>
<td class="GB1F3LJFQ GB1F3LJKO" title="" align="left" style="height: 19px; width: 63px;">
<div style="height: 19px; width: 63px;">
<div class="gwt-HTML">

</div>
</div>
</td>
<td class="GB1F3LJFQ GB1F3LJKO" title="" align="left" style="height: 19px; width: 28px;">
<div style="height: 19px; width: 28px;">
<div class="gwt-HTML">

</div>
</div>
</td>
<td class="GB1F3LJFQ GB1F3LJKO" title="Agency_Group_0_Agency_Group_Member_0_dan_all_features_6@xxx.com" align="left" style="height: 19px; width: 146px;">
<div style="height: 19px; width: 146px;">
<div class="gwt-HTML">Agency_Group_0_Agency_Group_Member_0_dan_all_features_6@xxx.com</div>
</div>
</td>
</tr>
</tbody>
</table>

复选框的xpath是: / HTML /体/格[6] / DIV /表/ tbody的/ TR [2] / TD [2] / DIV / DIV / DIV / DIV /表/ tbody的/ TR [2] / TD / DIV / DIV /格/ DIV [1] / DIV /表[10] / tbody的/ TR / TD [1]

用户名栏的xpath是: / HTML /体/格[6] / DIV /表/ tbody的/ TR [2] / TD [2] / DIV / DIV / DIV / DIV /表/ tbody的/ TR [2] / TD / DIV / DIV /格/ DIV [1] / DIV /表[10] / tbody的/ TR / TD [3]

我的代码循环遍历名称列表,并且应该单击与该名称关联的复选框: 在成员列表中的名称:         打印'单击与%s​​'%名称关联的复选框         chrome.find_element_by_xpath(“// tr [td [contains(text()='”+ name +“')]] / td [1]”)。click()

我得到的错误是: selenium.common.exceptions.InvalidSelectorException:消息:无效的选择器:由于以下错误,无法找到具有xpath表达式的元素// tr [td [contains(text()='Agency_Group_0_Agency_Group_Member_0_dan_all_features_6')]] / td [1] : SyntaxError:无法对'Document'执行'evaluate':字符串'// tr [td [contains(text()='Agency_Group_0_Agency_Group_Member_0_dan_all_features_6')]] / td [1]'不是有效的XPath表达式。   (会话信息:chrome = 48.0.2564.116)

我的问题是: 如何单击与名称关联的复选框?

1 个答案:

答案 0 :(得分:1)

您未正确使用contains()。替换:

//tr[td[contains(text()='" + name + "')]]/td[1]

使用:

//tr[td[contains(text(), '" + name + "')]]/td[1]
相关问题