Issue
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 class="ui-lib-sidebar-grid__content">
<div class="ui-lib-margin-b_md ui-lib-margin-t_md totalResultsText">
<p class="totalResultsText">Showing 1-10 of 316 results</p>
</div>
<div class="">
<div class="ui-lib-category-list ui-lib-category-list-dummy">
<div class="ui-lib-category-list-item">
<div class="ui-lib-category-list-item__content">
<div class="ui-lib-category-list-item__content-header">
<div class="ui-lib-category-list-item-type"><span class="ui-lib-category-list-item-type__label">Service</span></div>
</div>
<h4 class="ui-lib-link__heading ui-lib-bold">Request for Collection and Transfer of Green Waste from Residential Areas</h4>
<div class="ui-lib-category-list-item__description">Through this service, you can request the collection and transfer of green wastes from residential areas.</div>
<div class="ui-lib-category-list-item__tags">
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_grey ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Abu Dhabi Waste Management Center</span></div>
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Expat</span></div>
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Visitor</span></div>
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Emirati</span></div>
</div>
</div>
</div>
<div class="ui-lib-category-list-item">
<div class="ui-lib-category-list-item__content">
<div class="ui-lib-category-list-item__content-header">
<div class="ui-lib-category-list-item-type"><span class="ui-lib-category-list-item-type__label">Service</span></div>
</div>
<h4 class="ui-lib-link__heading ui-lib-bold">Request for Collection and Transfer of Large-Scale Wastes from Residential Areas</h4>
<div class="ui-lib-category-list-item__description">Through this service, you can request the collection and transfer of wastes in large amounts from residential areas.</div>
<div class="ui-lib-category-list-item__tags">
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_grey ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Abu Dhabi Waste Management Center</span></div>
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Expat</span></div>
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Visitor</span></div>
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Emirati</span></div>
</div>
</div>
</div>
<div class="ui-lib-category-list-item">
<div class="ui-lib-category-list-item__content">
<div class="ui-lib-category-list-item__content-header">
<div class="ui-lib-category-list-item-type"><span class="ui-lib-category-list-item-type__label">Service</span></div>
</div>
<h4 class="ui-lib-link__heading ui-lib-bold">Request for Licencing a Private Notary Office</h4>
<div class="ui-lib-category-list-item__description">Apply for licencing a private notary office</div>
<div class="ui-lib-category-list-item__tags">
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_grey ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Abu Dhabi Judicial Department</span></div>
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Business</span></div>
</div>
</div>
</div>
<div class="ui-lib-category-list-item">
<div class="ui-lib-category-list-item__content">
<div class="ui-lib-category-list-item__content-header">
<div class="ui-lib-category-list-item-type"><span class="ui-lib-category-list-item-type__label">Service</span></div>
</div>
<h4 class="ui-lib-link__heading ui-lib-bold">Request for Transferring the Licence of a Private Notary from One Office to Another</h4>
<div class="ui-lib-category-list-item__description">Apply for transferring the licence of a private notary from one office to another</div>
<div class="ui-lib-category-list-item__tags">
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_grey ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Abu Dhabi Judicial Department</span></div>
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Business</span></div>
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Emirati</span></div>
</div>
</div>
</div>
<div class="ui-lib-category-list-item">
<div class="ui-lib-category-list-item__content">
<div class="ui-lib-category-list-item__content-header">
<div class="ui-lib-category-list-item-type"><span class="ui-lib-category-list-item-type__label">Service</span></div>
</div>
<h4 class="ui-lib-link__heading ui-lib-bold">Request for Cancelling the Registration of a Private Notary Office</h4>
<div class="ui-lib-category-list-item__description">Apply for cancelling the registration of a private notary office</div>
<div class="ui-lib-category-list-item__tags">
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_grey ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Abu Dhabi Judicial Department</span></div>
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Business</span></div>
</div>
</div>
</div>
<div class="ui-lib-category-list-item">
<div class="ui-lib-category-list-item__content">
<div class="ui-lib-category-list-item__content-header">
<div class="ui-lib-category-list-item-type"><span class="ui-lib-category-list-item-type__label">Service</span></div>
</div>
<h4 class="ui-lib-link__heading ui-lib-bold">Request to Open a New Branch for a Private Notary Office</h4>
<div class="ui-lib-category-list-item__description">Apply for opening a new branch of a private notary office</div>
<div class="ui-lib-category-list-item__tags">
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_grey ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Abu Dhabi Judicial Department</span></div>
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Business</span></div>
</div>
</div>
</div>
<div class="ui-lib-category-list-item">
<div class="ui-lib-category-list-item__content">
<div class="ui-lib-category-list-item__content-header">
<div class="ui-lib-category-list-item-type"><span class="ui-lib-category-list-item-type__label">Service</span></div>
</div>
<h4 class="ui-lib-link__heading ui-lib-bold">Request for Renewal of a Private Notary Office Registration Card</h4>
<div class="ui-lib-category-list-item__description">Apply for renewal of a private notary office registration card</div>
<div class="ui-lib-category-list-item__tags">
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_grey ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Abu Dhabi Judicial Department</span></div>
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Business</span></div>
</div>
</div>
</div>
<div class="ui-lib-category-list-item">
<div class="ui-lib-category-list-item__content">
<div class="ui-lib-category-list-item__content-header">
<div class="ui-lib-category-list-item-type"><span class="ui-lib-category-list-item-type__label">Service</span></div>
</div>
<h4 class="ui-lib-link__heading ui-lib-bold">Request Nuisance Pest Control</h4>
<div class="ui-lib-category-list-item__description">Through this service, you can request nuisance pest control for ants, termites, lizards, German cockroaches, ticks, fleas and bed bugs.</div>
<div class="ui-lib-category-list-item__tags">
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_grey ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Abu Dhabi Waste Management Center</span></div>
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Emirati</span></div>
</div>
</div>
</div>
<div class="ui-lib-category-list-item">
<div class="ui-lib-category-list-item__content">
<div class="ui-lib-category-list-item__content-header">
<div class="ui-lib-category-list-item-type"><span class="ui-lib-category-list-item-type__label">Service</span></div>
</div>
<h4 class="ui-lib-link__heading ui-lib-bold">Request Stray Animal Impounding</h4>
<div class="ui-lib-category-list-item__description">Through this service, you can request and schedule impounding stray animals, such as camels, cows, cats, dogs, etc.</div>
<div class="ui-lib-category-list-item__tags">
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_grey ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Abu Dhabi Waste Management Center</span></div>
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Expat</span></div>
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Visitor</span></div>
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Emirati</span></div>
</div>
</div>
</div>
<div class="ui-lib-category-list-item">
<div class="ui-lib-category-list-item__content">
<div class="ui-lib-category-list-item__content-header">
<div class="ui-lib-category-list-item-type"><span class="ui-lib-category-list-item-type__label">Service</span></div>
</div>
<h4 class="ui-lib-link__heading ui-lib-bold">Request Vector Pest Control Service</h4>
<div class="ui-lib-category-list-item__description">Through this service, you can request vector pest control of mosquitos, flies, rodents and American cockroaches.</div>
<div class="ui-lib-category-list-item__tags">
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_grey ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Abu Dhabi Waste Management Center</span></div>
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Expat</span></div>
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Visitor</span></div>
<div class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable" tabindex="0" role="button"><span class="ui-lib-tag__text">Emirati</span></div>
</div>
</div>
</div>
</div>
</div>
<div class="ui-lib-margin-t_ ui-lib-margin-b_xl search-pagination">
<div class="ui-lib-pagination ui-lib-pagination_align-center">
<div class="ui-lib-pagination__item ui-lib-pagination__item_page ui-lib-pagination__item_current" tabindex="0" role="button" aria-label="">1</div>
<div class="ui-lib-pagination__item ui-lib-pagination__item_page" tabindex="0" role="button" aria-label="">2</div>
<div class="ui-lib-pagination__item ui-lib-pagination__item_page" tabindex="0" role="button" aria-label="">3</div>
<div class="ui-lib-pagination__item ui-lib-pagination__item_page" tabindex="0" role="button" aria-label="">4</div>
<div class="ui-lib-pagination__item ui-lib-pagination__item_page" tabindex="0" role="button" aria-label="">5</div>
<div class="ui-lib-pagination__item ui-lib-pagination__item_dots" tabindex="0" role="button" aria-label="Next 3 Pages"> </div>
<div class="ui-lib-pagination__item ui-lib-pagination__item_page" tabindex="0" role="button" aria-label="">32</div>
<div class="ui-lib-pagination__item ui-lib-pagination__item_nav" tabindex="0" role="button" aria-label="Next"> </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?
Solution
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());
Answered By - undetected Selenium
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.