我需要计算元素的数量,在本例中是每个客户持有的产品数量。我试图通过计算"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>发布于 2016-07-04 19:01:53
请尝试以下代码:
// 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);希望这能有所帮助
发布于 2016-07-06 18:49:37
试试这个:
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();https://stackoverflow.com/questions/38180554
复制相似问题