Question

I want find the numberof lis items (li) in html below:

<div class="li-control-block">
    <div class="inner">
        <a class="scroll-left" href="?slideId=7">
            <img width="59" height="30" alt="btn-hero-promo-slider-left" src="/c/images/btn-hero-promo-slider-left.png">
        </a>
        <ul class="slides-8">
            <li class="first">
                <a href="#">1</a>
            </li>
            <li class="">
                <a href="#">2</a>
            </li>
            <li class="">
                <a href="#">3</a>
            </li>
            <li class="">
                <a href="#">4</a>
            </li>
            <li class="">
                <a href="#">5</a>
            </li>
            <li class="">
                <a href="#">6</a>
            </li>
            <li class="">
                <a href="#">7</a>
            </li>
            <li class="last on">
        </ul>
            <a class="scroll-right" href="?slideId=1">
    </div>
</div>

I have tried code below but that comes back with "12345678" which gives me size of 1.

So how can I get the size to be equal to 8 ?

List<WebElement> list=null;

    list = driver.findElements(By.cssSelector(".slides-8"));
    int length = list.size();
Was it helpful?

Solution

Try doing

WebElement temp = driver.findElement(By.classname("slides-8"));
List<WebElement> list = temp.findElement(By.xpath(".//li"));
System.out.println(list.size());

OTHER TIPS

Your example works as expected; your code requests the number of elements which have a class of "slides-8", which is the list container. Then you ask for how many elements that is, and it's one -- just one container.

Either apply a CSS class to each list element, or ask the container for the count of its elements.

you can try using cssSelectors as well,

List<WebElement> list = driver.findElements(By.cssSelector(".slides-8 > li"));
int length = list.size();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top