【问题标题】:Selenium XPATH I have a list of checkboxes in a column. I am trying to select the checkbox in row 2Selenium XPATH 我在一列中有一个复选框列表。我正在尝试选择第 2 行中的复选框
【发布时间】: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


【解决方案1】:

这是从第二行获取复选框的方法:

//table[@id='match_configuration_add_possible_tab_match_rules_tb_match_rules']//tbody/tr[2]//input[@type='checkbox']

关于“前”和“祖”:

这是因为您搜索的复选框是之后(在)您的表格元素中。但是像你一样使用“preceding”,你正在搜索一个复选框BEFORE你的表格元素(在HTML代码中)。 类似“祖先”。您正在搜索作为 tr 的表元素“ABOVE”的元素。除非您的表格是另一个表格的一部分,否则这将导致没有合适的元素。相反,您可以使用“descendant”,它将搜索“WITHIN”您的元素。

【讨论】:

  • 这行得通,感谢祖先和前面的解释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2014-03-11
  • 1970-01-01
  • 1970-01-01
  • 2015-12-15
  • 1970-01-01
相关资源
最近更新 更多