找到你要的答案

Q:Selenium XPATH I have a list of checkboxes in a column. I am trying to select the checkbox in row 2

Q:硒的XPath我有一列复选框列表。我想选择2排的复选框

I have a list of checkboxes in a column. I am trying to select the checkbox from the 2nd row. I have tried the following XPATH but it selects all of the checboxes.

//table[@id="match_configuration_add_possible_tab_match_rules_tb_match_rules"]//input[@type = "checkbox"]

The HTML is:

    <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>

I would like to select the checkbox which has cell-gwt-uid-299 I cannot use UID-299 in the XPATH because this value is dynamic. The number changes when you visit the page.

I have also tried the following XPATH using ancestor:

//table[@id="match_configuration_add_possible_tab_match_rules_tb_match_rules"]/ancestor::tr[1]//input[@type = "checkbox"]

and I have tried using preceding. In Developer Tools window a checkbox is being highlighted but it is from a different table:

//table[@id="match_configuration_add_possible_tab_match_rules_tb_match_rules"]/preceding::tr[1]//input[@type = "checkbox"]

strange using preceding it is trying to select a checkbox from a different table. Here is the HTML which was highlighted in develop tools when i use preceding:

    <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>

What XPATH could I use?

我有一个列中的复选框列表。我想从第二行的复选框。我试过以下XPath但是选取所有的checboxes。

//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>

I would like to select the checkbox which has cell-gwt-uid-299 I cannot use UID-299 in the XPATH because this value is dynamic. The number changes when you visit the page.

我也曾试过下面的XPath使用祖先:

//table[@id="match_configuration_add_possible_tab_match_rules_tb_match_rules"]/ancestor::tr[1]//input[@type = "checkbox"]

我尝试使用前面。在开发人员工具窗口的复选框突出,但它是从一个不同的表:

//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我可以用吗?

answer1: 回答1:

Here's how to get the checkbox from the second row:

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

Regarding the "preceding" and "ancestor":

This is because the checkbox you were searching is AFTER(within) your table-element. But using "preceding" as you did, you were searching for a checkbox BEFORE your table element (in the HTML code). Similar for "ancestor". You were searching for an element "ABOVE" your table element that is a tr. Unless your table is part of another table, this will result in no fitting element. Instead you could have used "descendant", which will search "WITHIN" your element.

这里是如何从第二行的复选框:

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

关于“前”和“祖”:

This is because the checkbox you were searching is AFTER(within) your table-element. But using "preceding" as you did, you were searching for a checkbox BEFORE your table element (in the HTML code). Similar for "ancestor". You were searching for an element "ABOVE" your table element that is a tr. Unless your table is part of another table, this will result in no fitting element. Instead you could have used "descendant", which will search "WITHIN" your element.

css  selenium  xpath  selenium-webdriver