javaweb-scrapinghtmlunit

struggling to click on link within htmlunit


I am having a problem clicking on a link within htmlunit. I went through the api on the site(which I didn't really understand well) and looked at all the sample code I could find and am still having a problem with clicking on links.

Here's the top of the error messsage(its pretty large, if you want I can submit it all)

"page2 = link2.click() Exception class=[net.sourceforge.htmlunit.corejs.javascript.JavaScriptException] com.gargoylesoftware.htmlunit.ScriptException: Sys.ArgumentOutOfRangeException: Sys.ArgumentOutOfRangeException: Value must be an integer. Parameter name: x Actual value was Infinity. "

The first page loads fine but when I click on the second link, I get this error(the link is javascript). Here's parts of my code

       page = webclient.getPage(url) 
   anchors1 =  page.getAnchors()
   for anchor in anchors1:
        if anchor.asText() == "2":
            link2 = anchor
            break
   page2 = link2.click()

If I do a print link2 I get: HtmlAnchor[<a href="javascript:__doPostBack('ctl00$MainContent$gvSearchResults','Page$2')">]

At first I thought maybe the HtmlAnchor was a problem and I had to remove it but then I looked at other sample code and they seem to have their links end up in the same format and it works.

So confused..please help :-)

Thanks in advance!


Solution

  • The problem is not in the code used to click on the link. It's in the JavaScript executed when the link is clicked. Either the JavaScript is buggy, or the JavaScript interpreter used by HtmlUnit has a problem running it.

    The problem seems to be with a parameter x which has the Infinity value during the execution.