gpt4 book ai didi

java - 使用selenium和java获取div的总数

转载 作者:行者123 更新时间:2023-11-30 01:46:00 27 4
gpt4 key购买 nike

下面是 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"))

最佳答案

统计并打印 <div class="box3 top"> 的总数<div class="box">内您可以使用以下任一 Locator Strategies :

  • css选择器:

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.box div.box3.top"))).size());
  • xpath:

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@class='box']//div[@class='box3 top']"))).size());

关于java - 使用selenium和java获取div的总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57993735/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com