我有这个代码示例:
如何在<div>
父亲的硒中获得多少<div>
?
我有这段代码:
List<String> productName = new ArrayList<String>();
List<WebElement> divElement = driver.findElements(By.cssSelector("div.list-group-item llamada-ajax-TA.text-center"));
for(WebElement w : divElement) {
productName.add(w.getText());
}
System.out.print(productName);
但是只打印数组中的内容保存,我需要知道有多少<div>
个子元素。
答案 0 :(得分:0)
您可以使用xpath选择器代替不可读的css选择器:
// means: find div that has class 'list-group' and get all it's direct div children
List<WebElement> children = driver.findElements(By.xpath("//div[@class='list-group']/div"))
System.out.println("Size: " + children.size()); // Size: 8
答案 1 :(得分:0)
List<WebElement> childrenList = getDriver().findElement(By.className("list-group")).findElements(By.cssSelector("div.list-group-item.llamada-ajax-TA.text-center"));
System.out.println("Size: " + childrenList.size());
这将找到由class locator&#34; list-group&#34;
定义的父项下的子项