div
我定位的HTML页面的s如下所示:
<div class="white-row1">
<div class="results">
<div class="profile">
<a href="hrefThatIWant.com" class>
<img src = "http://imgsource.jpg" border="0" width="150" height="150 alt>
</a>
</div>
</div>
</div>
<div class="white-row2">
// same content as the div above
</div>
我想在列表中的每个div中取消收集href。
这是我当前的代码:
List<HtmlAnchor> profileDivLinks = (List)htmlPage.getByXPath("//div[@class='profile']//@href");
for(HtmlAnchor link:profileDivLinks)
{
System.out.println(link.getHrefAttribute());
}
这是我收到的错误(在for语句的第一行):
Exception in thread "main" java.lang.ClassCastException: com.gargoylesoftware.htmlunit.html.DomAttr cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlAnchor
您认为问题是什么?
问题是您要获取一个属性,然后将该属性转换为锚点。我猜想,对您的代码进行最少更改的解决方案就是修改XPath以返回锚点:
htmlPage.getByXPath("//div[@class='profile']//a");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句