我有一个方法:
public void clickCheckBox(int index) {
List<WebElement> sizeList = getSizeList();
WebElement checkbox = sizeList.get(index);
Actions action = new Actions(driver);
action.moveToElement(checkbox).click().build().perform();
}
此方法获取复选框列表的索引,然后使用Actions类检查复选框。我这样做是因为我正在使用的页面会在选中此复选框时触发事件,所以只需执行一个简单的查找复选框并单击不起作用(事件不会触发)。
我上面的代码没有选中复选框,我不知道为什么。任何见解都会很棒。如果我没有提供足够的信息,请告诉我。
编辑:
这是获取列表大小的方法:
public List<WebElement> getSizeList(){
List<WebElement> sizeList = body.findElement(By.cssSelector("ul")).findElements(By.cssSelector("li"));
if(null==sizeList) {
// oops, couldn't find the element
LOGGER.error("Failed to locate the 'li' element for the action button");
return Collections.emptyList();
}
return sizeList;
}
以下是HTML的一些内容:
<ul>
<li>
<input type="checkbox">
<a href="www.url.com" title="8">8</a>
<span class="count">(1,037)</span>
</li>
<li>
<input type="checkbox">
<a href="www.url.com" title="10">10</a>
<span class="count">(1,047)</span>
</li> ...
答案 0 :(得分:1)
您找到了li但不输入的元素(Checkbox)
//get the checkboxes size with below command
List<WebElement> sizeList = body.findElements(By.cssSelector("ul>li>input"));
or
List<WebElement> sizeList = body.findElements(By.cssSelector("input[type='checkbox']"));
现在使用上面的列表进行操作。
for(WebElement eachChkBox : sizeList) {
new Actions(driver).click(eachChkBox).perform();
}