如何使用'值'选择多个复选框网页中的属性使用selenium webdriver从excel文件获取数据

时间:2016-03-21 09:34:29

标签: selenium-webdriver

我是硒的新手。我想使用selenium webdriver在我的网页中选择复选框,方法是从本地excel表中获取数据。我为所有的单选按钮实现了这个功能,但是我想要选择的这些复选框没有' id'属性。它们仅以“价值”来区分。

HTML代码为:

    <input type="hidden" value="1" name="%%Surrogate_Competitors"/>
    <label>
    <input type="checkbox" title="Competitors" value="HP (Servers)" name="Competitors"/>
    HP (Servers)
    </label>
    <br/>
    <label>
    <input type="checkbox" title="Competitors" value="Sun (Servers)" name="Competitors"/>
    Sun (Servers)
    </label>
    <br/>
    <label>
    <input type="checkbox" title="Competitors" value="HP (Storage)" name="Competitors"/>
    HP (Storage)
    </label>
    <br/>
    ............
    ............

我尝试使用此代码选择这些复选框:

String Competitors=sh.getCell(column,1).getContents();
String delims = "[,]";
String[] Competitor = Competitors.split(delims);

for(int i=0;i<Competitor.length;i++) {
    //to select competitors
    driver.findElement(By.cssSelector("input[value="+Competitor[i]+"]")).click();
}

当我运行此代码时,会生成一条错误消息,如下所示:

  

给定的选择器输入[value = HP(存储)]无效或无效   不会导致WebElement。

请让我知道如何克服这个问题,并选中复选框。

1 个答案:

答案 0 :(得分:0)

请检查css标识符,您可能需要为值添加引号:

 driver.findElement(By.cssSelector("input[value='" + Competitor[i] + "']"))

stack post必须有用