如何使用Selenium WebDriver从Excel读取数据来选择多个复选框?

时间:2018-07-04 04:56:42

标签: java selenium

我正在使用编码UI为Web应用程序创建一些测试用例,与此同时,我遇到了一个问题。该页面包含3个带有多个复选框的表,我无法单击从excel中读取数据的复选框。有人帮助我找到了解决方案 这是我的html代码:

表1-HTML:

<table id="ContentPlaceHolder1_ctl04_cbl_CCPEC01_01">
    <tbody>
       <tr>
          <td>
             <input 
                id="ContentPlaceHolder1_ctl04_cbl_CCPEC01_01_0"
                type="checkbox" 
                name="ctl00$ContentPlaceHolder1$ctl04$cbl_CCPEC01_01$0"
                value=" Ambu-bag     ">
             <label for="ContentPlaceHolder1_ctl04_cbl_CCPEC01_01_0"> Ambu-bag   </label>
          </td>
          <td>
             <input
                id="ContentPlaceHolder1_ctl04_cbl_CCPEC01_01_1"
                type="checkbox" 
                name="ctl00$ContentPlaceHolder1$ctl04$cbl_CCPEC01_01$1" 
                value=" apnea monitor    ">
             <label 
                for="ContentPlaceHolder1_ctl04_cbl_CCPEC01_01_1"> apnea monitor  </label>
          </td>
          <td>
       </tr>
    </tbody>
</table>

表2-HTML:

<table id="ContentPlaceHolder1_ctl04_cbl_CCPOP01_01">
   //table 2
   <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>
      </tr>
   </tbody>
</table>

这是我的示例代码。但是不能正常工作。

表1-代码:

String valueEquipmentChecklist = data.getEquipmentChecklist().get(rowCnt);//data reading from excel

List<WebElement> equipChecklist = driver.findElements(By.xpath("//input[@type='checkbox']"));
List<String> equipmentChecklistList = new ArrayList<String>(Arrays.asList(valueEquipmentChecklist.split(",")));

for (String equipmentChecklistCheck : equipmentChecklistList) {
    for (WebElement equipmentChecklistChk : equipChecklist) {

        if (equipmentChecklistChk.getAttribute("value").equalsIgnoreCase(equipmentChecklistCheck)) {
                if (!equipmentChecklistChk.isSelected()) {
                    equipmentChecklistChk.click();
                }
        }
    }
}

表2-代码:

String valueOngoingPrecuations = data.getOngoingPrecuations().get(rowCnt);

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)) {
            if (!ongoingPrecuationsChk.isSelected()) {
                ongoingPrecuationsChk.click();
            }   
        }
    }
}

1 个答案:

答案 0 :(得分:0)

根据HTML,您提供的复选框值包含空格,可能在excel文件中该空格不存在,因此您应在比较之前删除复选框值的空格,或使用“ 包含”代替“ 等于