python - 确认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)
但我需要遍历并找到确切的文本,然后单击复选框。
解决方案
您想要的定位器是 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”文本。我会更进一步,将其放入一个方法中,并将标签文本作为参数传递,以便您可以使其可重用。
推荐阅读
- c# - 当我点击逃生时,暂停菜单不会激活
- python - 如何在字典 python 中使用 */+- 等运算符和字符串?
- javascript - Select2 多个值(标签)
- java - 如何解决有关环境变量的启动 PyCharm 的错误不指向有效的 JVM 安装?
- android - Editext 出现时隐藏在键盘后面
- laravel - “.../storage/logs”中没有现有目录,无法创建:只读文件系统
- javascript - 我如何对本机清理功能做出反应?
- c++ - 如何显示我输入到文本文件中的所有数据?
- android - 将数据从 StatelessWidget 传递到 StatefulWidget
- javascript - 如何在 JavaScript 中删除撇号?