简体   繁体   中英

Selecting a check box from a list of check boxes

In one scenario, I have to select a Check box with text "ALL". Here is the sample HTML code:

<tbody id="modulePermissionDatagridId_data" class="ui-datatable-data ui-widget-content">
    <tr class="ui-widget-content ui-datatable-even" role="row" data-ri="0">
    <tr class="ui-widget-content ui-datatable-odd" role="row" data-ri="1">
        <td role="gridcell">
            <div id="modulePermissionDatagridId:1:j_idt124" class="ui-chkbox ui-widget multipleSelectionChkBox">
                <div class="ui-helper-hidden-accessible">
                    <input id="modulePermissionDatagridId:1:j_idt124_input" type="checkbox" onchange="handleChkBoxValueChange(this)" name="modulePermissionDatagridId:1:j_idt124_input"/>
                </div>
                **<div class="ui-chkbox-box ui-widget ui-corner-all ui-state-default">**
                    <span class="ui-chkbox-icon ui-c"/>
                </div>
                <span class="ui-chkbox-label">ALL</span>
            </div>
        </td>
    </tr>

Note: In the above HTML, the check box is pointing to the 'div' element above 'span'.

Currently i am using the following code which is working fine:

WebElement perm = driver.findElement(By.id("modulePermissionDatagridId_data"));
List<WebElement> permno = perm.findElements(By.tagName("tr"));

int i=0;
for(WebElement wb : permno)
{
    String permName = wb.getText();
    System.out.println(permName);
    //for (int i=0; i< permno.size(); i++)
    //{
    System.out.println(i);
    if (permName.equals("ALL"))
    {
        System.out.println(permName);
        System.out.println(i);
        driver.findElement(By.xpath("//*[@id='modulePermissionDatagridId:"+i+":j_idt124']/div[2]")).click();
        //break;
    }
    //}
    i++;
}

But i want to simplify this code. So i tried the following XPATH which is not working:

driver.findElement(By.xpath("//tbody[@id='modulePermissionDatagridId_data']/tr[./td/div[span='ALL']]/div")).click();

Any suggestions please?

这个xpath为我工作:

//span[contains(text(), 'ALL') and @class='ui-chkbox-label']/../div/input

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM