[英]Click on check box after confirming text in html table which is dynamic
斷言文本后,我需要單擊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”文本替換為表中所需的任何標簽。 我將更進一步,將其放入方法中並將標簽文本作為參數傳遞,以便可以重用。
您可以輕松地針對所需文本單擊關聯的復選框 ,例如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()
現在,您可以調用將任何標簽作為字符串參數傳遞的函數,如下所示:
click_item("ACCESSGROUP_BULKDELETE")
# or
click_item("ACCESSGROUP_DELETE")
# or
click_item("ACCESSGROUP_NEW")
如果要查找與某些給定文本相對應的相關復選框,請使用以下表達式:
//td[text()='ACCESSGROUP_BULKDELETE']/preceding-sibling::td/input
演示:
在上面的表達式中
preceding-sibling
是XPath Axis ,它匹配所有“同級”-具有相同父級並出現在當前元素上方的DOM元素。
text()
是XPath函數 ,它返回當前節點的文本內容
下面是java中的代碼。...您可以將其與Python關聯。 希望這可以幫助
String rowXpath = "//*[@id='root']//table//tbody//tr[INDEX]" //xpath to find no of rows
noOfRows = context.driver.find_elements(By.XPATH, "//*[@id='root']//table//tbody//tr") //get the count of row
for(int i=1; i<= noOfRows; i++){
String currXpath = rowXpath.replace(INDEX, i) //update xpath to point to curent row
currXpath = currXpath+/td[2] //Find xpath of chkBox for current tr in loop
String chkBoxXpath = currXpath+"/td[1]" //xpath to point to 1st td under current tr in loop
String currentText = context.driver.find_element(By.XPATH, currXpath).getText(); //find text in 2nd td current row in loop
if(currentText.equals("ACCESSGROUP_BULKDELETE")){ //Compare with any text of your interest //chk if text found above macthes with your interested text
context.driver.find_element(By.XPATH, chkBoxXpath).click(); //click on the 1st td(chkbox) for the tr which contains your text
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.