简体   繁体   中英

How to click on multiple checkboxes in selenium webdriver?

I'm trying to check multiple Checkbox in a table via reading data from Excel , it is actually able to read data from excel and locate the Checkbox but it does not check/click the checkbox. The ongoingPrecuationsChk.click(); is not working and it does not show any exception(skips execution of the line), can somebody explain to me why it's not checking/clicking the checkbox? Below is my html code:

<table id="ContentPlaceHolder1_ctl04_cbl_CCPOP01_01">
  <tbody>
    <tr>
     <td>
    <input id="ContentPlaceHolder1_ctl04_cbl_CCPOP01_01_0"
 type="checkbox"
     name="ctl00$ContentPlaceHolder1$ctl04$cbl_CCPOP01_01$0" 
value=" Universal    ">
<label for="ContentPlaceHolder1_ctl04_cbl_CCPOP01_01_0"> Universal   </label></td>
<td><input id="ContentPlaceHolder1_ctl04_cbl_CCPOP01_01_1" 
type="checkbox" 
name="ctl00$ContentPlaceHolder1$ctl04$cbl_CCPOP01_01$1"
 value=" Aspiration  ">
<label for="ContentPlaceHolder1_ctl04_cbl_CCPOP01_01_1"> Aspiration  </label></td>
    <td><input id="ContentPlaceHolder1_ctl04_cbl_CCPOP01_01_2" 
type="checkbox" name="ctl00$ContentPlaceHolder1$ctl04$cbl_CCPOP01_01$2" 
value=" Respiratory ">
<label for="ContentPlaceHolder1_ctl04_cbl_CCPOP01_01_2"> Respiratory </label></td>
    </tr>
    </tbody>

I have tried the following code:

String valueOngoingPrecuations = data.getOngoingPrecuations().get(rowCnt);//data reading from excel(Aspiration,Universal)
            List<WebElement> ongoingPrecuations = driver.findElements(By.xpath("//input[@type='checkbox']"));
            List<String> ongoingPrecuationsList = new ArrayList<String>(
                    Arrays.asList(valueOngoingPrecuations.split(",")));
            for (String ongoingPrecuationsCheck : ongoingPrecuationsList) {
                for (WebElement ongoingPrecuationsChk : ongoingPrecuations) {
                    if (ongoingPrecuationsChk.getAttribute("value").equalsIgnoreCase(ongoingPrecuationsCheck)) {
                        ongoingPrecuationsChk.click();

                    }
                }
            }

Use xpath based on id of tbody like below.

//tbody[contains(@id,'tableBodyId')]//input

This will give you list of checkboxes. Then use

WebElement btn = driver.findElement(By.xpath("xpath"));

btn.click();

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