【问题标题】:how to select a checkbox in a table depending on the row value?如何根据行值选择表格中的复选框?
【发布时间】:2016-06-15 21:41:38
【问题描述】:

我正在使用自动化 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/body/div[6]/div/table/tbody/tr[2]/td[2]/div/div/div/div/table/tbody/tr[2]/td/div/div/div /div[1]/div/table[10]/tbody/tr/td[1]

用户名列的 xpath 是: /html/body/div[6]/div/table/tbody/tr[2]/td[2]/div/div/div/div/table/tbody/tr[2]/td/div/div/div /div[1]/div/table[10]/tbody/tr/td[3]

我的代码循环遍历一个名称列表,它应该单击与该名称关联的复选框: 对于成员列表中的名称: print '单击与 %s 关联的复选框' %name 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: 无法对“文档”执行“评估”:字符串 '//tr[td[contains(text()='Agency_Group_0_Agency_Group_Member_0_dan_all_features_6')]]/td[1]' 不是有效的 XPath 表达式。 (会话信息:chrome=48.0.2564.116)

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

【问题讨论】:

  • 可能有时您需要将鼠标悬停在它上面,然后继续单击它。您知道有时对象不会直接启用,直到鼠标悬停在它上面。

标签: python selenium


【解决方案1】:

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

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

与:

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

【讨论】:

  • 这很有帮助。感谢您的答复。这条路让我意识到它行不通。然而,核心思想对于实际解决问题非常有用。我最终使用的代码是: chrome.find_element_by_xpath("//tr[td[@title='" + name + "']]/td[1]/div").click()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-18
  • 2015-08-17
  • 1970-01-01
  • 2015-03-21
  • 1970-01-01
相关资源
最近更新 更多