无法获取 div 中的元素数

Unable to get the count of elements inside the div

HTML:

<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="" style=""><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"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMid slice" data-key="" class="ui-lib-category-list-item-type__icon ui-lib-category-list-item-type__icon_service ui-lib-icon" width="24" height="24" viewBox="0 0 24 24"><title></title><circle cx="12" cy="12" r="12" fill-rule="evenodd"></circle></svg><span class="ui-lib-category-list-item-type__label">Service</span></div></div><a class="ui-lib-link ui-lib-link_default" target="_self" aria-label="Request for Collection and Transfer of Green Waste from Residential Areas" rel="noopener noreferrer" href="https://www.tamm.abudhabi/en/aspects-of-life/environmentagriculture/Waste-Management/WasteCollectionandTransportation/RequestforCollectionandTransferofGreenWastefromResidentialAreas"><h4 class="ui-lib-link__heading ui-lib-bold">Request for Collection and Transfer of Green Waste from Residential Areas</h4></a><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 tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_grey ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Abu Dhabi Waste Management Center</span></div><div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Expat</span></div><div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Visitor</span></div><div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><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"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMid slice" data-key="" class="ui-lib-category-list-item-type__icon ui-lib-category-list-item-type__icon_service ui-lib-icon" width="24" height="24" viewBox="0 0 24 24"><title></title><circle cx="12" cy="12" r="12" fill-rule="evenodd"></circle></svg><span class="ui-lib-category-list-item-type__label">Service</span></div></div><a class="ui-lib-link ui-lib-link_default" target="_self" aria-label="Request for Collection and Transfer of Large-Scale Wastes from Residential Areas" rel="noopener noreferrer" href="https://www.tamm.abudhabi/en/aspects-of-life/environmentagriculture/Waste-Management/WasteCollectionandTransportation/RequestforCollectionandTransferofLargeScaleWastesfromResidentialAreas"><h4 class="ui-lib-link__heading ui-lib-bold">Request for Collection and Transfer of Large-Scale Wastes from Residential Areas</h4></a><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 tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_grey ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Abu Dhabi Waste Management Center</span></div><div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Expat</span></div><div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Visitor</span></div><div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><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"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMid slice" data-key="" class="ui-lib-category-list-item-type__icon ui-lib-category-list-item-type__icon_service ui-lib-icon" width="24" height="24" viewBox="0 0 24 24"><title></title><circle cx="12" cy="12" r="12" fill-rule="evenodd"></circle></svg><span class="ui-lib-category-list-item-type__label">Service</span></div></div><a class="ui-lib-link ui-lib-link_default" target="_self" aria-label="Request for Licencing a Private Notary Office" rel="noopener noreferrer" href="https://www.tamm.abudhabi/en/aspects-of-life/lawregulations/notarypublic/NotaryPublicLicensing/RequestforLicencingaPrivateNotaryOffice"><h4 class="ui-lib-link__heading ui-lib-bold">Request for Licencing a Private Notary Office</h4></a><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 tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_grey ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Abu Dhabi Judicial Department</span></div><div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><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"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMid slice" data-key="" class="ui-lib-category-list-item-type__icon ui-lib-category-list-item-type__icon_service ui-lib-icon" width="24" height="24" viewBox="0 0 24 24"><title></title><circle cx="12" cy="12" r="12" fill-rule="evenodd"></circle></svg><span class="ui-lib-category-list-item-type__label">Service</span></div></div><a class="ui-lib-link ui-lib-link_default" target="_self" aria-label="Request for Transferring the Licence of a Private Notary from One Office to Another" rel="noopener noreferrer" href="https://www.tamm.abudhabi/en/aspects-of-life/lawregulations/notarypublic/NotaryPublicLicensing/requestfortransferringthelicenceofaprivatenotaryfromoneofficetoanother"><h4 class="ui-lib-link__heading ui-lib-bold">Request for Transferring the Licence of a Private Notary from One Office to Another</h4></a><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 tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_grey ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Abu Dhabi Judicial Department</span></div><div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Business</span></div><div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><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"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMid slice" data-key="" class="ui-lib-category-list-item-type__icon ui-lib-category-list-item-type__icon_service ui-lib-icon" width="24" height="24" viewBox="0 0 24 24"><title></title><circle cx="12" cy="12" r="12" fill-rule="evenodd"></circle></svg><span class="ui-lib-category-list-item-type__label">Service</span></div></div><a class="ui-lib-link ui-lib-link_default" target="_self" aria-label="Request for Cancelling the Registration of a Private Notary Office" rel="noopener noreferrer" href="https://www.tamm.abudhabi/en/aspects-of-life/lawregulations/notarypublic/NotaryPublicLicensing/requestforcancellingtheregistrationofaprivatenotaryoffice"><h4 class="ui-lib-link__heading ui-lib-bold">Request for Cancelling the Registration of a Private Notary Office</h4></a><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 tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_grey ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Abu Dhabi Judicial Department</span></div><div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><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"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMid slice" data-key="" class="ui-lib-category-list-item-type__icon ui-lib-category-list-item-type__icon_service ui-lib-icon" width="24" height="24" viewBox="0 0 24 24"><title></title><circle cx="12" cy="12" r="12" fill-rule="evenodd"></circle></svg><span class="ui-lib-category-list-item-type__label">Service</span></div></div><a class="ui-lib-link ui-lib-link_default" target="_self" aria-label="Request to Open a New Branch for a Private Notary Office" rel="noopener noreferrer" href="https://www.tamm.abudhabi/en/aspects-of-life/lawregulations/notarypublic/NotaryPublicLicensing/requestforopeninganewbranchofaprivatenotaryoffice"><h4 class="ui-lib-link__heading ui-lib-bold">Request to Open a New Branch for a Private Notary Office</h4></a><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 tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_grey ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Abu Dhabi Judicial Department</span></div><div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><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"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMid slice" data-key="" class="ui-lib-category-list-item-type__icon ui-lib-category-list-item-type__icon_service ui-lib-icon" width="24" height="24" viewBox="0 0 24 24"><title></title><circle cx="12" cy="12" r="12" fill-rule="evenodd"></circle></svg><span class="ui-lib-category-list-item-type__label">Service</span></div></div><a class="ui-lib-link ui-lib-link_default" target="_self" aria-label="Request for Renewal of a Private Notary Office Registration Card" rel="noopener noreferrer" href="https://www.tamm.abudhabi/en/aspects-of-life/lawregulations/notarypublic/NotaryPublicLicensing/requestforrenewalofaprivatenotaryofficeregistrationcard"><h4 class="ui-lib-link__heading ui-lib-bold">Request for Renewal of a Private Notary Office Registration Card</h4></a><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 tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_grey ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Abu Dhabi Judicial Department</span></div><div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><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"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMid slice" data-key="" class="ui-lib-category-list-item-type__icon ui-lib-category-list-item-type__icon_service ui-lib-icon" width="24" height="24" viewBox="0 0 24 24"><title></title><circle cx="12" cy="12" r="12" fill-rule="evenodd"></circle></svg><span class="ui-lib-category-list-item-type__label">Service</span></div></div><a class="ui-lib-link ui-lib-link_default" target="_self" aria-label="Request Nuisance Pest Control" rel="noopener noreferrer" href="https://www.tamm.abudhabi/en/aspects-of-life/environmentagriculture/Waste-Management/PublicHealthPestControl/RequestNuisancePestControl"><h4 class="ui-lib-link__heading ui-lib-bold">Request Nuisance Pest Control</h4></a><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 tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_grey ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Abu Dhabi Waste Management Center</span></div><div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><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"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMid slice" data-key="" class="ui-lib-category-list-item-type__icon ui-lib-category-list-item-type__icon_service ui-lib-icon" width="24" height="24" viewBox="0 0 24 24"><title></title><circle cx="12" cy="12" r="12" fill-rule="evenodd"></circle></svg><span class="ui-lib-category-list-item-type__label">Service</span></div></div><a class="ui-lib-link ui-lib-link_default" target="_self" aria-label="Request Stray Animal Impounding" rel="noopener noreferrer" href="https://www.tamm.abudhabi/en/aspects-of-life/environmentagriculture/Waste-Management/PublicHealthPestControl/RequestStrayAnimalImpounding"><h4 class="ui-lib-link__heading ui-lib-bold">Request Stray Animal Impounding</h4></a><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 tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_grey ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Abu Dhabi Waste Management Center</span></div><div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Expat</span></div><div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Visitor</span></div><div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><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"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMid slice" data-key="" class="ui-lib-category-list-item-type__icon ui-lib-category-list-item-type__icon_service ui-lib-icon" width="24" height="24" viewBox="0 0 24 24"><title></title><circle cx="12" cy="12" r="12" fill-rule="evenodd"></circle></svg><span class="ui-lib-category-list-item-type__label">Service</span></div></div><a class="ui-lib-link ui-lib-link_default" target="_self" aria-label="Request Vector Pest Control Service" rel="noopener noreferrer" href="https://www.tamm.abudhabi/en/aspects-of-life/environmentagriculture/Waste-Management/PublicHealthPestControl/RequestVectorPestControlService"><h4 class="ui-lib-link__heading ui-lib-bold">Request Vector Pest Control Service</h4></a><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 tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_grey ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Abu Dhabi Waste Management Center</span></div><div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Expat</span></div><div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><span class="ui-lib-tag__text">Visitor</span></div><div tabindex="0" role="button" class="ui-lib-tag ui-lib-tag_size-default ui-lib-tag_blue ui-lib-tag_hoverable ui-lib-tag_clickable"><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" style=""><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" aria-label="" role="button" tabindex="0">1</div><div class="ui-lib-pagination__item ui-lib-pagination__item_page" aria-label="" role="button" tabindex="0">2</div><div class="ui-lib-pagination__item ui-lib-pagination__item_page" aria-label="" role="button" tabindex="0">3</div><div class="ui-lib-pagination__item ui-lib-pagination__item_page" aria-label="" role="button" tabindex="0">4</div><div class="ui-lib-pagination__item ui-lib-pagination__item_page" aria-label="" role="button" tabindex="0">5</div><div class="ui-lib-pagination__item ui-lib-pagination__item_dots" aria-label="Next 3 Pages" role="button" tabindex="0"><svg tabindex="0" version="1.1" xmlns="http://www.w3.org/2000/svg" data-key="" class="ui-lib-pagination__item-icon ui-lib-icon" width="24" height="24" viewBox="0 0 24 24"><title></title><circle cx="3.25" cy="12" r="3.25"></circle><circle cx="12" cy="12" r="3.25"></circle><circle cx="20.75" cy="12" r="3.25"></circle></svg></div><div class="ui-lib-pagination__item ui-lib-pagination__item_page" aria-label="" role="button" tabindex="0">32</div><div class="ui-lib-pagination__item ui-lib-pagination__item_nav" aria-label="Next" role="button" tabindex="0"><svg tabindex="0" version="1.1" xmlns="http://www.w3.org/2000/svg" data-key="" class="ui-lib-pagination__item-icon ui-lib-icon" width="24" height="24" viewBox="0 0 24 24"><title></title><path d="M23.987,12a2.411,2.411,0,0,0-.814-1.8L11.994.361a1.44,1.44,0,0,0-1.9,2.162l8.637,7.6a.25.25,0,0,1-.165.437H1.452a1.44,1.44,0,0,0,0,2.88H18.563a.251.251,0,0,1,.165.438l-8.637,7.6a1.44,1.44,0,1,0,1.9,2.161L23.172,13.8A2.409,2.409,0,0,0,23.987,12Z"></path></svg></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();
System.out.println("Total Number of Search results " +TotalSearchCountSize);

当运行代码输出元素个数显示为0

理想情况下,它应该 return 编辑 10,但是 findElements 从来没有 return 任何 fault/error。如果它 finds elements 则它将 return a list of web element,否则它将 return 0

所以首先调试这个问题,你应该使用

driver.findElement 而不是 driver.findElements,现在很可能 driver.findElement 将 return HTML 中的单个或第一个匹配节点,如果没有找到的话那么它会导致相应的错误。

万一 return 发现了一个 nosuchElement 异常,那么您可以使用以下几点进一步调试:

NoSuchElementException

检查步骤:

  1. 检查它是否在任何 iframe/frame/frameset.

    解决方法:先切换到iframe/frame/frameset,再与该网页元素交互

  2. 检查它是否在任何 shadow-root.

    解决方法:使用JavaScriptExecutor('return document.querySelector来return编辑一个网页元素,然后进行相应的操作。

  3. 确保在与元素交互之前正确呈现该元素。放一些 hardcoded delayExplicit wait 然后再试一次。

    解决方法: Thread.sleep(5000) 或者

    wait = new WebDriverWait(driver, Duration.ofSeconds(30)); wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[@class='ui-lib-sidebar-grid__content']//div[contains(@class,'ui-lib-category-list ui-lib-category-list-dummy')]/div"))).size();

  4. 如果您已重定向到 new tab/ or new windows 而您还没有 切换 到那个特定的 new tab/new window,否则您可能会得到NoSuchElement异常。

    解决方法:先切换到相关window/tab

  5. 如果您已切换到 iframe 并且新的所需元素不在同一 iframe 上下文中,那么首先 switch to default content 然后与其交互。

    解决方法:切换到默认内容,然后切换到相应的iframe。

输出显示为0 instance is initiating the search for the elements even before the desired WeElements have properly within the HTML DOM. In such cases you have to synchronize the WebDriver with the Browsing Context using .


解决方案

要打印元素的数量,您必须为 and you can use the following 引入 WebDriverWait:

  • 使用 xpath:

    System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[@class='ui-lib-sidebar-grid__content']//div[contains(@class,'ui-lib-category-list ui-lib-category-list-dummy')]/div"))).size());