Issue
I want to get the link attribute from the below list of elements, Its returning null when executing
<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>
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.
Solution
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()));
Answered By - undetected Selenium
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.