【问题标题】:Selenium Xpath I am trying to select the checkbox in col2 where name is in col1 - using following axis or descendantSelenium Xpath 我正在尝试选择 col2 中名称在 col1 中的复选框 - 使用跟随轴或后代
【发布时间】:2016-03-10 16:17:19
【问题描述】:

我有一个包含一些行和列的 HTML 表格。
表格中有一行在第 1 列中显示文本 “CRM”,在第 2 列中显示了复选框。 我想从这一行中选择 Checkbox,而 Checkbox 在第 2 列中

我想选中第 1 列包含文本“CRM”的复选框

我想构建 Xpath,我想我可以使用跟随轴或后代轴到达从第 1 列找到文本“CRM”的复选框

我需要一些帮助来构建 Xpath。 这是我迄今为止尝试过的:

此 Xpath 将找到第 2 列中的所有复选框:

`//table[@class="myflextable"]//tr//td[text()="CRM"]/following::tr/td[2]//input`

此 XPath 将在第 1 列中找到包含文本“CRM”的单元格。这是一个好的开始,因为我想从文本“CRM”开始

//table[@class="myflextable"]//tr//td[text()="CRM"]

如何转到复选框所在的下一列?

HTML 是:

<table class="myflextable">
    <colgroup>
    <tbody>
    <tr class="myflextableheader">
    <tr>
        <td>
<span class="gwt-CheckBox">
<input id="gwt-uid-1377" type="checkbox" value="on" tabindex="0" checked=""/>
<label for="gwt-uid-1377"/>
</span>
        </td>
        <td>CRM</td>
    </tr>
    <tr>
        <td>
<span class="gwt-CheckBox">
<input id="gwt-uid-1378" type="checkbox" value="on" tabindex="0" checked=""/>
<label for="gwt-uid-1378"/>
</span>
        </td>
        <td>ESCR</td>
    </tr>
    <tr>
        <td>
<span class="gwt-CheckBox">
<input id="gwt-uid-1379" type="checkbox" value="on" tabindex="0" checked=""/>
<label for="gwt-uid-1379"/>
</span>
        </td>
        <td>ORCHARD</td>
    </tr>
    <tr>
        <td>
<span class="gwt-CheckBox">
<input id="gwt-uid-1380" type="checkbox" value="on" tabindex="0" checked=""/>
<label for="gwt-uid-1380"/>
</span>
        </td>
        <td>Edit_test</td>
    </tr>
    </tbody>
</table>

</body>
</html>

谢谢, 里亚兹

【问题讨论】:

  • 要不要勾选id为gwt-uid-1378"//table//td[text()='CRM']/following::input[1]"的复选框?
  • 是或 id 1377。ID 是动态的,所以我不能使用 [@id="1377"] 我想按文本搜索
  • 那么上面的代码就可以了,你试过了吗?
  • 是的,现在刚试过。有用。谢谢!
  • 将其作为答案,我可以将其标记为已接受

标签: python-2.7 selenium xpath selenium-webdriver


【解决方案1】:

您可以在包含您想要的文本的 td 之后获取第一个以下输入。

//table//td[text()='CRM']/following::input[1]

或者也可以使用表类名:

 //table[@class='myflextable']//td[text()='CRM']/following::input[1]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多