确认动态html表中的文本后单击复选框 [英] Click on check box after confirming text in html table which is dynamic
问题描述
我需要在断言文本后单击 HTML 表中的复选框.下面是html.
<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>
<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">在访问组管理中启用批量删除按钮</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">在访问组详细信息页面中启用删除按钮</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]')打印(table_id)# 获取表中的所有行#rows = table_id.find_elements(By.TAG_NAME, "tr")#for row in row:#permission = row.find_elements(By.TAG_NAME, 'td')[1]#print (permission.text)
但我需要遍历并找到确切的文本,然后单击复选框.
您可以轻松地单击与所需文本相关的复选框,例如ACCESSGROUP_BULKDELETE
、ACCESSGROUP_DELETE
、ACCESSGROUP_NEW
等编写如下函数:
def click_item(item_text):WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[.='" + item_text + "']//preceding::td[1]//label[@for]"))).点击()
现在您可以调用将任何标签作为字符串参数传递的函数,如下所示:
click_item("ACCESSGROUP_BULKDELETE")# 或者click_item("ACCESSGROUP_DELETE")# 或者click_item(ACCESSGROUP_NEW")
I need to click on the check box in the HTML table after asserting the text. Below is the 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>
I was able to get text from the TR with the code mentioned
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)
But I need to iterate through and find exact text and then click the check box.
You can easily click on the associated check box with respect to the desired text e.g. ACCESSGROUP_BULKDELETE
, ACCESSGROUP_DELETE
, ACCESSGROUP_NEW
, etc writing a function as follows:
def click_item(item_text):
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[.='" + item_text + "']//preceding::td[1]//label[@for]"))).click()
Now you can call the function passing any of the labels as a string arguments as follows:
click_item("ACCESSGROUP_BULKDELETE")
# or
click_item("ACCESSGROUP_DELETE")
# or
click_item("ACCESSGROUP_NEW")
这篇关于确认动态html表中的文本后单击复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!