I want to get the link attribute from the below list of elements, Its returning null when executing
<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>
Code trials:
List<WebElement> FirstFive = 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());
System.out.println(FirstFive.size());
for (WebElement webElement : FirstFive){
String link = webElement.getAttribute("href");
System.out.println(link);
}
I need to store the value in a variable.
CodePudding user response:
The link attribute are basically the value of href attributes and to extract those you can use the following solution:
System.out.println(driver.findElements(By.xpath("//div[@class='ui-lib-sidebar-grid__content']//div[contains(@class,'ui-lib-category-list ui-lib-category-list-dummy')]/div//a")).stream().map(element->element.getAttribute("href")).collect(Collectors.toList()));