使用Capybara从表中的特定行和列中选择复选框



我是Capybara的新手,我需要选择在同一列中的复选框,其中是星期三。我如何选择它?我尝试了几种方法,但我仍然得到这个错误,元素没有找到。

HTML部分如下所示:

<table class="grid">
            <thead>
                <tr>
                    <th colspan="1" rowspan="1" style="width: 56px;">From</th>
                    <th colspan="1" rowspan="1" style="width: 56px;">To</th>
                    <th colspan="1" rowspan="1">Monday</th>
                    <th colspan="1" rowspan="1">Tuesday</th>
                    <th colspan="1" rowspan="1">Wednesday</th>
                    <th colspan="1" rowspan="1">Thursday</th>
                    <th colspan="1" rowspan="1">Friday</th>
                    <th colspan="1" rowspan="1">Saturday</th>
                    <th colspan="1" rowspan="1">Sunday</th>
                    <th colspan="1" rowspan="1" style="width: 18px;"></th>
                </tr>
            </thead>
            <tbody>
                <tr class="t-forminjector" id="rowInjector_1470ffb567f" style=
                "background-image: none; background-color: rgba(0, 0, 0, 0);">
                    <td colspan="1" rowspan="1" xmlns=
                    "http://www.w3.org/1999/xhtml">
                        <input name="t:formdata" type="hidden" value=
                        "H4sIAAIDOPgvsNqt">
                        <div class="timepicker"><input class=
                        "timepicker-input patternDailyStart hasDatepicker" id=
                        "patternDailyStart_1470ffb567f" name=
                        "patternDailyStart_1470ffb567f" type="text" value="">
                        <img alt="..." class="ui-datepicker-trigger" src=
                        "clock.png" title="..."></div><img alt="" class=
                        "t-error-icon" id="patternDailyStart_1470ffb567f_icon" src=
                        "spacer.gif" style="display: none;">
                    </td>
                    <td colspan="1" rowspan="1" xmlns=
                    "http://www.w3.org/1999/xhtml">
                        <div class="dailyEnd">
                        <div class="timepicker"><input class=
                        "timepicker-input patternDailyEnd hasDatepicker" id=
                        "patternDailyEnd_1470ffb567f" name=
                        "patternDailyEnd_1470ffb567f" type="text" value="">
                        <img alt="..." class="ui-datepicker-trigger" src=
                        "clock.png" title="..."></div><img alt="" class=
                        "t-error-icon" id="patternDailyEnd_1470ffb567f_icon" src=
                        "spacer.gif" style="display: none;"></div>
                    </td>
                    <td colspan="1" rowspan="1" xmlns=
                    "http://www.w3.org/1999/xhtml"><input id="checkbox_1470ffb567f"
                    name="checkbox_1470ffb567f" type="checkbox"> <img alt="" class=
                    "t-error-icon" id="checkbox_1470ffb567f_icon" src=
                    "/mpromoter/assets/8fe900a87e3fabc9/core/spacer.gif" style=
                    "display: none;"></td>
                    <td colspan="1" rowspan="1" xmlns=
                    "http://www.w3.org/1999/xhtml"><input id=
                    "checkbox_0_1470ffb567f" name="checkbox_0_1470ffb567f" type=
                    "checkbox"> <img alt="" class="t-error-icon" id=
                    "checkbox_0_1470ffb567f_icon" src=
                    "/mpromoter/assets/8fe900a87e3fabc9/core/spacer.gif" style=
                    "display: none;"></td>
                    <td colspan="1" rowspan="1" xmlns=
                    "http://www.w3.org/1999/xhtml"><input id=
                    "checkbox_1_1470ffb567f" name="checkbox_1_1470ffb567f" type=
                    "checkbox"> <img alt="" class="t-error-icon" id=
                    "checkbox_1_1470ffb567f_icon" style="display: none;"></td>
                    <td colspan="1" rowspan="1"><input id="checkbox_2_1470ffb567f"
                    name="checkbox_2_1470ffb567f" type="checkbox"> <img alt=""
                    class="t-error-icon" id="checkbox_2_1470ffb567f_icon" src=
                    "/mpromoter/assets/8fe900a87e3fabc9/core/spacer.gif" style=
                    "display: none;"></td>
                    <td colspan="1" rowspan="1"><input id="checkbox_3_1470ffb567f"
                    name="checkbox_3_1470ffb567f" type="checkbox"> <img alt=""
                    class="t-error-icon" id="checkbox_3_1470ffb567f_icon" src=
                    "/mpromoter/assets/8fe900a87e3fabc9/core/spacer.gif" style=
                    "display: none;"></td>
                    <td colspan="1" rowspan="1"><input id="checkbox_4_1470ffb567f"
                    name="checkbox_4_1470ffb567f" type="checkbox"> <img alt=""
                    class="t-error-icon" id="checkbox_4_1470ffb567f_icon" src=
                    "/mpromoter/assets/8fe900a87e3fabc9/core/spacer.gif" style=
                    "display: none;"></td>
                    <td colspan="1" rowspan="1"><input id="checkbox_5_1470ffb567f"
                    name="checkbox_5_1470ffb567f" type="checkbox"> <img alt=""
                    class="t-error-icon" id="checkbox_5_1470ffb567f_icon" src=
                    "/mpromoter/assets/8fe900a87e3fabc9/core/spacer.gif" style=
                    "display: none;"></td>
                    <td colspan="1" rowspan="1" xmlns=
                    "http://www.w3.org/1999/xhtml">
                        <a href="#" id="removerowlink_1470ffb567f"><img alt="" src=
                        "/mpromoter/assets/8fe900a87e3fabc9/ctx/images/delete.ico"
                        title="Remove"></a>
                    </td>
                </tr>
                <tr class="t-forminjector" id="rowInjector">
                    <td></td>
                </tr>
            </tbody>
        </table>
当我复制xpath时,它看起来像这样://*[@id="checkbox_1_1470ffb567f"]

复选框的id每次都是不同的(最后这些数字每次打开页面时都会改变)。但是checkbox_1部分是一样的

您可以使用contains()函数:

//*[contains(@id, "checkbox_1_")]

最新更新