1

Below is the HTML page

<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>

I want the total number of div count which is equal to <div class="box3 top"> from the <div class="box">

I tried with below code, but its count all the div irrespective of class, let me know how can I count the div based on the class name?

WebElement resultGrid = driver.findElement(By.xpath("/html/body/div[4]/div/div/div[1]/div[4]"))
            List<WebElement>totalRow = resultGrid.findElements(By.tagName("div"))
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Prabu
  • 3,550
  • 9
  • 44
  • 85

1 Answers1

2

To count and print the total number of <div class="box3 top"> within <div class="box"> you can use either of the following Locator Strategies:

  • cssSelector:

    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());
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352