如何使用 Selenium 和 Java 从 Web 元素列表中获取 href 值
How to get the href value out of a list of webelements with Selenium and Java
我想从下面的元素列表中获取 link 属性,执行时返回 null
<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>
代码试验:
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);
}
我需要将值存储在变量中。
link 属性基本上是 href 属性的值,要提取这些属性,您可以使用以下解决方案:
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()));
我想从下面的元素列表中获取 link 属性,执行时返回 null
<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>
代码试验:
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);
}
我需要将值存储在变量中。
link 属性基本上是 href 属性的值,要提取这些属性,您可以使用以下解决方案:
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()));