首页 > 解决方案 > 确认html表格中的动态文本后单击复选框

问题描述

在断言文本后,我需要单击 HTML 表中的复选框。下面是html。

<div class="k-widget k-grid ">
    <div class="k-grid-header" style="padding: 0px 16px 0px 0px;">
        <div class="k-grid-header-wrap">
            <table>
                <colgroup>
                    <col width="50px">
                    <col>
                    <col>
                </colgroup>
                <thead>
                    <tr>
                        <th aria-sort="" colspan="1" rowspan="1" class="k-header checkbox-grid-column"><input id="c3c07f7e-5119-4a36-9f67-98fa4d21fa07" type="checkbox" class="k-checkbox"><label class="k-checkbox-label" for="c3c07f7e-5119-4a36-9f67-98fa4d21fa07"></label></th>
                        <th aria-sort="" colspan="1" rowspan="1" class="k-header"><a class="k-link">Permission</a></th>
                        <th aria-sort="" colspan="1" rowspan="1" class="k-header"><a class="k-link">Description</a></th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
    <div class="k-grid-container">
        <div class="k-grid-content k-virtual-content">
            <div style="position: relative;">
                <table tabindex="-1" class="k-grid-table">
                    <colgroup>
                        <col width="50px">
                        <col>
                        <col>
                    </colgroup>
                    <tbody>
                        <tr class="k-master-row">
                            <td colspan="1" class="checkbox-grid-column"><input id="c8711bab-702a-43b9-8a75-02ad06a8baa3" type="checkbox" class="k-checkbox"><label class="k-checkbox-label" for="c8711bab-702a-43b9-8a75-02ad06a8baa3"></label></td>
                            <td>ACCESSGROUP_BULKDELETE</td>
                            <td colspan="1">Enable Bulk Delete button in access group management</td>
                        </tr>
                        <tr class="k-master-row k-alt">
                            <td colspan="1" class="checkbox-grid-column"><input id="a029bc1e-53d8-4328-89ce-6640363d515a" type="checkbox" class="k-checkbox"><label class="k-checkbox-label" for="a029bc1e-53d8-4328-89ce-6640363d515a"></label></td>
                            <td>ACCESSGROUP_DELETE</td>
                            <td colspan="1">Enable Delete Button in the access group details page</td>
                        </tr>
                        <tr class="k-master-row">
                            <td colspan="1" class="checkbox-grid-column"><input id="645f8474-9840-48e2-a02c-112178aaf5e2" type="checkbox" class="k-checkbox"><label class="k-checkbox-label" for="645f8474-9840-48e2-a02c-112178aaf5e2"></label></td>
                            <td>ACCESSGROUP_NEW</td>

我能够使用提到的代码从 TR 中获取文本

table_id = context.driver.find_elements(By.XPATH, '//*[@id="root"]//table//tbody//tr//td[1]')
    print (table_id)
    # get all of the rows in the table
    #rows = table_id.find_elements(By.TAG_NAME, "tr")
    #for row in rows:
        #permission = row.find_elements(By.TAG_NAME, 'td')[1]
        #print (permission.text)

但我需要遍历并找到确切的文本,然后单击复选框。

标签: pythonseleniumselenium-webdriverxpathwebdriverwait

解决方案


您想要的定位器是 XPath,因为 XPath 允许您根据包含的文本查找元素。

//tr[./td[.='ACCESSGROUP_BULKDELETE']]//input
^ find a TR
    ^ that has a child TD that contains the desired text
                                      ^ then find the descendant INPUT of that TR

您可以使用表格中所需的任何标签替换“ACCESSGROUP_BULKDELETE”文本。我会更进一步,将其放入一个方法中,并将标签文本作为参数传递,以便您可以使其可重用。


推荐阅读