HTML:
<div ><div ><p >Showing 1-10 of 316 results</p></div><div style=""><div ><div ><div ><div ><div ><svg version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMid slice" data-key="" width="24" height="24" viewBox="0 0 24 24"><title></title><circle cx="12" cy="12" r="12" fill-rule="evenodd"></circle></svg><span >Service</span></div></div><a 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 >Request for Collection and Transfer of Green Waste from Residential Areas</h4></a><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 ><svg version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMid slice" data-key="" width="24" height="24" viewBox="0 0 24 24"><title></title><circle cx="12" cy="12" r="12" fill-rule="evenodd"></circle></svg><span >Service</span></div></div><a 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 >Request for Collection and Transfer of Large-Scale Wastes from Residential Areas</h4></a><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 ><svg version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMid slice" data-key="" width="24" height="24" viewBox="0 0 24 24"><title></title><circle cx="12" cy="12" r="12" fill-rule="evenodd"></circle></svg><span >Service</span></div></div><a 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 >Request for Licencing a Private Notary Office</h4></a><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 ><svg version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMid slice" data-key="" width="24" height="24" viewBox="0 0 24 24"><title></title><circle cx="12" cy="12" r="12" fill-rule="evenodd"></circle></svg><span >Service</span></div></div><a 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 >Request for Transferring the Licence of a Private Notary from One Office to Another</h4></a><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 ><svg version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMid slice" data-key="" width="24" height="24" viewBox="0 0 24 24"><title></title><circle cx="12" cy="12" r="12" fill-rule="evenodd"></circle></svg><span >Service</span></div></div><a 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 >Request for Cancelling the Registration of a Private Notary Office</h4></a><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 ><svg version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMid slice" data-key="" width="24" height="24" viewBox="0 0 24 24"><title></title><circle cx="12" cy="12" r="12" fill-rule="evenodd"></circle></svg><span >Service</span></div></div><a 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 >Request to Open a New Branch for a Private Notary Office</h4></a><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 ><svg version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMid slice" data-key="" width="24" height="24" viewBox="0 0 24 24"><title></title><circle cx="12" cy="12" r="12" fill-rule="evenodd"></circle></svg><span >Service</span></div></div><a 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 >Request for Renewal of a Private Notary Office Registration Card</h4></a><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 ><svg version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMid slice" data-key="" width="24" height="24" viewBox="0 0 24 24"><title></title><circle cx="12" cy="12" r="12" fill-rule="evenodd"></circle></svg><span >Service</span></div></div><a 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 >Request Nuisance Pest Control</h4></a><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 ><svg version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMid slice" data-key="" width="24" height="24" viewBox="0 0 24 24"><title></title><circle cx="12" cy="12" r="12" fill-rule="evenodd"></circle></svg><span >Service</span></div></div><a 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 >Request Stray Animal Impounding</h4></a><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 ><svg version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMid slice" data-key="" width="24" height="24" viewBox="0 0 24 24"><title></title><circle cx="12" cy="12" r="12" fill-rule="evenodd"></circle></svg><span >Service</span></div></div><a 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 >Request Vector Pest Control Service</h4></a><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 style=""><div ><div aria-label="" role="button" tabindex="0">1</div><div aria-label="" role="button" tabindex="0">2</div><div aria-label="" role="button" tabindex="0">3</div><div aria-label="" role="button" tabindex="0">4</div><div aria-label="" role="button" tabindex="0">5</div><div aria-label="Next 3 Pages" role="button" tabindex="0"><svg tabindex="0" version="1.1" xmlns="http://www.w3.org/2000/svg" data-key="" 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 aria-label="" role="button" tabindex="0">32</div><div aria-label="Next" role="button" tabindex="0"><svg tabindex="0" version="1.1" xmlns="http://www.w3.org/2000/svg" data-key="" 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>
I used this code to get the number of elements
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);
When running the code the number of elements in output is shown as 0
CodePudding user response:
The output is shown as 0 as WebDriver instance is initiating the search for the elements even before the desired WeElements have properly rendered within the HTML DOM. In such cases you have to synchronize the WebDriver with the Browsing Context using WebDriverWait.
Solution
To print the number of elements you have to induce WebDriverWait for the visibilityOfAllElementsLocatedBy()
and you can use the following locator strategy:
Using 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());
CodePudding user response:
Ideally, it should have returned 10
, However findElements
never return any fault/error
. if it finds
the elements
then it will return a list of web element
, if not then it will return 0
So first to debug this issue, you should use
driver.findElement
instead of driver.findElements
, now most likely driver.findElement
will return a single or first matching node in the HTML, if nothing is found then it will result in appropriate error.
In case if it returns a nosuchElement found exception then further you can debug using the below point:
NoSuchElementException
Steps to check:
Check if it's in any
iframe/frame/frameset
.Solution: switch to iframe/frame/frameset first and then interact with this web element.
Check if it's in any
shadow-root
.Solution: Use
JavaScriptExecutor('return document.querySelector
to have returned a web element and then operates accordingly.Make sure that the element is rendered properly before interacting with it. Put some
hardcoded delay
orExplicit wait
and try again.Solution:
Thread.sleep(5000)
orwait = 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();
If you have redirected to a
new tab/ or new windows
and you have not switched to that particularnew tab/new window
, otherwise you will likely getNoSuchElement
exception.Solution: switch to the relevant window/tab first.
If you have switched to an iframe and the new desired element is not in the same iframe context then first
switch to default content
and then interact with it.Solution: switch to default content and then switch to the respective iframe.