下面是 HTML 页面<div class="box"> <div class="box3 disbox"></div> <div class="box3 top"><input type="hidden" value="38206" name="jdh0"> <div class="box3-1 bg1">1</div> <div class="clear"></div> </div> <div class="box3 top"><input type="hidden" value="38215" name="jdh1"> <div class="box3-1 bg1">2</div> <div class="clear"></div> </div> <div class="box3 top"><input type="hidden" value="38214" name="jdh2"> <div class="box3-1 bg1">3</div> <div class="clear"></div> </div> <div class="box3 top"><input type="hidden" value="38216" name="jdh3"> <div class="box3-1 bg1">4</div> <div class="clear"></div> </div> </div>我想要的 div 计数总数<div class="box3 top">等于<div class="box">我尝试使用下面的代码,但它会计算所有 div 的数量,而与类无关,请让我知道如何根据类名称计算 div 数量?WebElement resultGrid = driver.findElement(By.xpath("/html/body/div[4]/div/div/div[1]/div[4]")) List<WebElement>totalRow = resultGrid.findElements(By.tagName("div"))
1 回答
慕标琳琳
TA贡献1830条经验 获得超9个赞
<div class="box3 top">
要计算并打印其中的总数,<div class="box">
您可以使用以下定位器策略之一:
css选择器:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.box div.box3.top"))).size());
x路径:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@class='box']//div[@class='box3 top']"))).size());
添加回答
举报
0/150
提交
取消