'Counting the number of elements
I need to count the number of elements which in this case the number of products held by each customer. My attempt for solving this is by counting the number of "li" tag, then accessing "ticket-type". Any suggestion on how this can be achieved?:
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);
The .html code for this is shown below, where there are 5 products I need to count.
<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>
Solution 1:[1]
Please try the below code:
// 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);
Hope this helps
Solution 2:[2]
try this:
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();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | k.s. Karthik |
| Solution 2 | Sandeep |
