如何点击selenium webdriver中的多个复选框?

时间:2018-06-05 11:57:21

标签: java selenium-webdriver

我正在尝试通过从Excel读取数据来检查表中的多个Checkbox,它实际上能够从excel读取数据并找到Checkbox,但它不会检查/单击复选框。正在进行的PrecuationsChk.click();没有工作,它没有显示任何异常(跳过执行该行),有人可以向我解释为什么它不检查/单击复选框?以下是我的HTML代码:

<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>

我尝试过以下代码:

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();

                    }
                }
            }

1 个答案:

答案 0 :(得分:0)

使用基于tbody id的xpath,如下所示。

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

btn.click();

这将为您提供复选框列表。然后使用

>PROJECTDIR npm install --save youtube-api-search