如何使用 Selenium 和 Java 限制列表中的网络元素数量

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

我从以下 dom 中得到了这个列表,共有 10 个元素。我想将列表中的前 5 个元素存储在一个变量中。我该怎么做?

<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">&nbsp;</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">&nbsp;</div>
    </div>
</div>
</div>

代码试验:

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();

我用上面的代码得到了大小,现在我想保存前 5 个结果,我应该在这里使用哪个函数?

要将 List 中的元素数量限制为 5,您可以使用 Java8的stream() and limit() and you can use the following :

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());

尝试以下两种解决方案:


public void limit_array_list_size() {

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"));

List<WebElement> FinalList = FluentIterable.from(TotalSearchResults).limit(5).toList();

}

或者

List<WebElement> FinalList=TotalSearchResults.subList(0, 5);