【问题标题】:Click on check box after confirming text in html table which is dynamic确认html表格中的动态文本后单击复选框
【发布时间】:2019-08-02 01:47:22
【问题描述】:

在声明文本后,我需要单击 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)

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

【问题讨论】:

  • 您在寻找什么文字?你想点击哪个INPUT

标签: python selenium selenium-webdriver xpath webdriverwait


【解决方案1】:

您想要的定位器是 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”文本替换为表格中所需的任何标签。我会更进一步,将其放入一个方法中,并将标签文本作为参数传递,以便您可以使其可重用。

【讨论】:

  • 我能够得到它并在一个方法中替换它,因为文本可以是动态的并且可以从数据表中获取,主要关注的是在 [0] 上相同 TD 中的复选框上计时索引
  • 所以基本上在断言文本之后必须单击在 0 索引上的同一行上的复选框
  • 这就是它的作用。如果页面上不存在该文本,则不会找到该元素……这就是断言部分。您需要做的就是单击此定位器返回的INPUT 元素,这是您想要的复选框。
【解决方案2】:

您可以轻松单击与所需文本相关的复选框,例如ACCESSGROUP_BULKDELETEACCESSGROUP_DELETEACCESSGROUP_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")

【讨论】:

  • 工作就像一个魅力非常感谢您将开始关注学习如何制作更好的 CSS。再次感谢。
【解决方案3】:

如果您想找到与某些给定文本对应的相关复选框,请使用以下表达式:

//td[text()='ACCESSGROUP_BULKDELETE']/preceding-sibling::td/input

演示:

在上面的表达式中

【讨论】:

    【解决方案4】:

    下面是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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-16
      • 1970-01-01
      • 2012-08-03
      • 1970-01-01
      • 2012-11-01
      • 2013-02-21
      • 1970-01-01
      • 2021-10-29
      相关资源
      最近更新 更多