Home > Blockchain >  How can I limit the number of webelements in a list using Selenium and Java
How can I limit the number of webelements in a list using Selenium and Java

Time:04-21

I have this number of list from the following dom, There are total 10 elements. I want to store the first 5 elements from the list in a variable. How can i do it?

<div >
<div >
    <p >Showing 1-10 of 316 results</p>
</div>
<div >
    <div >
        <div >
            <div >
                <div >
                    <div ><span >Service</span></div>
                </div>
                <h4 >Request for Collection and Transfer of Green Waste from Residential Areas</h4>
                <div >Through this service, you can request the collection and transfer of green wastes from residential areas.</div>
                <div >
                    <div  tabindex="0" role="button"><span >Abu Dhabi Waste Management Center</span></div>
                    <div  tabindex="0" role="button"><span >Expat</span></div>
                    <div  tabindex="0" role="button"><span >Visitor</span></div>
                    <div  tabindex="0" role="button"><span >Emirati</span></div>
                </div>
            </div>
        </div>
        <div >
            <div >
                <div >
                    <div ><span >Service</span></div>
                </div>
                <h4 >Request for Collection and Transfer of Large-Scale Wastes from Residential Areas</h4>
                <div >Through this service, you can request the collection and transfer of wastes in large amounts from residential areas.</div>
                <div >
                    <div  tabindex="0" role="button"><span >Abu Dhabi Waste Management Center</span></div>
                    <div  tabindex="0" role="button"><span >Expat</span></div>
                    <div  tabindex="0" role="button"><span >Visitor</span></div>
                    <div  tabindex="0" role="button"><span >Emirati</span></div>
                </div>
            </div>
        </div>
        <div >
            <div >
                <div >
                    <div ><span >Service</span></div>
                </div>
                <h4 >Request for Licencing a Private Notary Office</h4>
                <div >Apply for licencing a private notary office</div>
                <div >
                    <div  tabindex="0" role="button"><span >Abu Dhabi Judicial Department</span></div>
                    <div  tabindex="0" role="button"><span >Business</span></div>
                </div>
            </div>
        </div>
        <div >
            <div >
                <div >
                    <div ><span >Service</span></div>
                </div>
                <h4 >Request for Transferring the Licence of a Private Notary from One Office to Another</h4>
                <div >Apply for transferring the licence of a private notary from one office to another</div>
                <div >
                    <div  tabindex="0" role="button"><span >Abu Dhabi Judicial Department</span></div>
                    <div  tabindex="0" role="button"><span >Business</span></div>
                    <div  tabindex="0" role="button"><span >Emirati</span></div>
                </div>
            </div>
        </div>
        <div >
            <div >
                <div >
                    <div ><span >Service</span></div>
                </div>
                <h4 >Request for Cancelling the Registration of a Private Notary Office</h4>
                <div >Apply for cancelling the registration of a private notary office</div>
                <div >
                    <div  tabindex="0" role="button"><span >Abu Dhabi Judicial Department</span></div>
                    <div  tabindex="0" role="button"><span >Business</span></div>
                </div>
            </div>
        </div>
        <div >
            <div >
                <div >
                    <div ><span >Service</span></div>
                </div>
                <h4 >Request to Open a New Branch for a Private Notary Office</h4>
                <div >Apply for opening a new branch of a private notary office</div>
                <div >
                    <div  tabindex="0" role="button"><span >Abu Dhabi Judicial Department</span></div>
                    <div  tabindex="0" role="button"><span >Business</span></div>
                </div>
            </div>
        </div>
        <div >
            <div >
                <div >
                    <div ><span >Service</span></div>
                </div>
                <h4 >Request for Renewal of a Private Notary Office Registration Card</h4>
                <div >Apply for renewal of a private notary office registration card</div>
                <div >
                    <div  tabindex="0" role="button"><span >Abu Dhabi Judicial Department</span></div>
                    <div  tabindex="0" role="button"><span >Business</span></div>
                </div>
            </div>
        </div>
        <div >
            <div >
                <div >
                    <div ><span >Service</span></div>
                </div>
                <h4 >Request Nuisance Pest Control</h4>
                <div >Through this service, you can request nuisance pest control for ants, termites, lizards, German cockroaches, ticks, fleas and bed bugs.</div>
                <div >
                    <div  tabindex="0" role="button"><span >Abu Dhabi Waste Management Center</span></div>
                    <div  tabindex="0" role="button"><span >Emirati</span></div>
                </div>
            </div>
        </div>
        <div >
            <div >
                <div >
                    <div ><span >Service</span></div>
                </div>
                <h4 >Request Stray Animal Impounding</h4>
                <div >Through this service, you can request and schedule impounding stray animals, such as camels, cows, cats, dogs, etc.</div>
                <div >
                    <div  tabindex="0" role="button"><span >Abu Dhabi Waste Management Center</span></div>
                    <div  tabindex="0" role="button"><span >Expat</span></div>
                    <div  tabindex="0" role="button"><span >Visitor</span></div>
                    <div  tabindex="0" role="button"><span >Emirati</span></div>
                </div>
            </div>
        </div>
        <div >
            <div >
                <div >
                    <div ><span >Service</span></div>
                </div>
                <h4 >Request Vector Pest Control Service</h4>
                <div >Through this service, you can request vector pest control of mosquitos, flies, rodents and American cockroaches.</div>
                <div >
                    <div  tabindex="0" role="button"><span >Abu Dhabi Waste Management Center</span></div>
                    <div  tabindex="0" role="button"><span >Expat</span></div>
                    <div  tabindex="0" role="button"><span >Visitor</span></div>
                    <div  tabindex="0" role="button"><span >Emirati</span></div>
                </div>
            </div>
        </div>
    </div>
</div>
<div >
    <div >
        <div  tabindex="0" role="button" aria-label="">1</div>
        <div  tabindex="0" role="button" aria-label="">2</div>
        <div  tabindex="0" role="button" aria-label="">3</div>
        <div  tabindex="0" role="button" aria-label="">4</div>
        <div  tabindex="0" role="button" aria-label="">5</div>
        <div  tabindex="0" role="button" aria-label="Next 3 Pages">&nbsp;</div>
        <div  tabindex="0" role="button" aria-label="">32</div>
        <div  tabindex="0" role="button" aria-label="Next">&nbsp;</div>
    </div>
</div>
</div>

Code trials:

List<WebElement> TotalSearchResults = driver.findElements(By.xpath("//div[@class='ui-lib-sidebar-grid__content']//div[contains(@class,'ui-lib-category-list ui-lib-category-list-dummy')]/div"));
int TotalSearchCountSize = TotalSearchResults.size();

I got the size with the above code, now I want to save the first 5 results, which function I should be using here?

CodePudding user response:

To limit the number of elements within the List to 5 you can use Java8's stream() and limit() and you can use the following locator strategy:

List<WebElement> TotalSearchResults = driver.findElements(By.xpath("//div[@class='ui-lib-sidebar-grid__content']//div[contains(@class,'ui-lib-category-list ui-lib-category-list-dummy')]/div")).stream().limit(5).collect(Collectors.toList());
  • Related