计算元素的数量

时间:2016-07-04 08:56:48

标签: selenium selenium-webdriver

我需要计算在这种情况下每个客户持有的产品数量的元素数量。我尝试解决这个问题的方法是计算" li"标签,然后访问" ticket-type"。关于如何实现这一点的任何建议?:

List<WebElement> labels = driver.findElements(By.xpath("//label[@class='ticket-type']"));
int ProdNum = labels.size();
String strI1 = Integer.toString(ProdNum);
System.out.print("Number of products on this card:" + strI1);
// getting the name of the last product
String LastProd = labels.get(labels.size() - 1).getText();
System.out.print("The last ordered product was:" + LastProd);

这个.html代码如下所示,我需要计算5种产品。

<div class="card-details row">
  <div class="pane base4">
  <div class="pane base4">
    <div class="vertical-accordion">
       <ul id="accordion-9" class="accordion">
          <li class="open-li">
             <a class="toggle-link open" href="#">
             <div class="accordion-drop" style="display: block;">
                <ul>
                   <li>
                      <label class="ticket-type">7 day megarider</label>
                      <label class="validity"> (7 day: 04 July to 10 July 2016 ) </label>
                   </li>
                   <li>
                   <li>
                   <li>
                   <li>
              </ul>
          </div>
        </li>
     </ul>
  </div>
</div>

2 个答案:

答案 0 :(得分:0)

请尝试以下代码:

// Getting total count of all li tag elements
List<WebElement> prodsCount = driver.findElements(By.xpath(".//div[@class='accordion-drop']/ul/li"));
System.out.println("Total number of products are " + prodsCount.size());

// Getting the last item name
String lastProdName = driver.findElement(By.xpath(".//div[@class='accordion-drop']/ul/li[last()]/lable[1]")).getText();
System.out.println("Last product name is " + lastProdName);

希望这有帮助

答案 1 :(得分:0)

试试这个:

    List<WebElement> numOfLiTag = driver.findElements(By.xpath(".//ul[@id='accordion-9']/li/div/ul/li[5]/label[1]"));
    int liTagcount = numOfLiTag.size();
    String abc = driver.findElement(By.xpath(".//ul[@id='accordion-9']/li/div/ul/li[5]/label[1]")).getText();