![](/img/trans.png)
[英]The value of attribute “href” associated with an element type “null” must not contain the '<' character
[英]href attribute of a li element coming up as null when there is data for the href
我試圖遍歷一個有序列表中的li元素的集合,然后從每個元素打印URL。
HTML:
<div id="global-atoz-navigation">
<nav role="navigation" aria-label="Suppliers">
<ol>
<li class="selected">
<span class="visuallyhidden">Suppliers starting with </span>
<strong>A</strong>
</li>
<li>
<span class="visuallyhidden">Suppliers starting with </span>
<a href="/g-cloud/suppliers?prefix=B">B</a>
</li>
<li>
<span class="visuallyhidden">Suppliers starting with </span>
<a href="/g-cloud/suppliers?prefix=C">C</a>
</li>
</ol>
</nav>
</div>
到目前為止,我的代碼:
WebElement navDiv = driver.findElement(By.id("global-atoz-navigation"));
List<WebElement> links = navDiv.findElements(By.tagName("li"));
for (WebElement link : links) {
System.out.println(link.getAttribute("href"));
}
但是由於某種原因,這為每個li元素打印了“ null”。 有什么想法嗎?
您的<li>
元素沒有href
屬性。 其中的<a>
元素可以。 您是否有理由不選擇navDiv.findElements(By.tagName("a"))
來查找它們? 或者,如果您想避免使用錨,請獲取所有錨並過濾出不良錨,或者獲取所有列表項並在每個li.findElements(By.tagName("a"))
執行另一個li.findElements(By.tagName("a"))
。
請注意, <li>
不是“鏈接”的縮寫。 這是“列表項”的縮寫。
鏈接位於“ anchor”或<a>
元素中。
從這里的答案引用:
“您沒有將其類型轉換為request.getAttribute()
將返回一個Object。”
嘗試使用它,看看是否可行:
String value = (String)link.getAttribute("href");
ErikE的回答也是正確的,您應該在其中獲取<a>標簽而不是<li>標簽的屬性
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.