html - HtmlUnit - Unable to get anchors from div -


the divs of html page targeting this:

<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 div above </div> 

i want scrap collect href in each div in list.

this current code:

list<htmlanchor> profiledivlinks = (list)htmlpage.getbyxpath("//div[@class='profile']//@href");  for(htmlanchor link:profiledivlinks) {     system.out.println(link.gethrefattribute()); } 

this error receiving (which goes on first line of statement):

exception in thread "main" java.lang.classcastexception: com.gargoylesoftware.htmlunit.html.domattr cannot cast com.gargoylesoftware.htmlunit.html.htmlanchor  

what think issue is?

the issue you're getting attribute , you're casting attribute anchor. guess solution minimal change code modifying xpath return anchor:

htmlpage.getbyxpath("//div[@class='profile']//a");  

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -