Search code examples
javahtmlhtmlunit

Click() Function Not Working HtmlUnit


Hey I'm having some trouble clicking a button to submit a comment on a thread using htmlUnit. I get the element by using this

HtmlPage newOne = webClient.getPage(string);
//System.out.println(newOne.asText());
HtmlTextArea area = (HtmlTextArea) newOne.getElementById("ctrl_message_html");
area.focus();
area.type('t');

newOne.getHtmlElementByAccessKey('s').click();

Which throws

com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException: 404 Not Found for http://forums.bukkit.org/threads/plugin-idea-to-get-tons-of-downloads.271185/members/jthort.90885864/post
at com.gargoylesoftware.htmlunit.WebClient.throwFailingHttpStatusCodeExceptionIfNecessary(WebClient.java:514)
at com.gargoylesoftware.htmlunit.WebClient.loadDownloadedResponses(WebClient.java:2067)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.doProcessPostponedActions(JavaScriptEngine.java:717)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.processPostponedActions(JavaScriptEngine.java:804)
at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1322)
at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1265)
at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1213)
at HTMLUnit.main(HTMLUnit.java:87)

When I print out the element, I get this

HtmlSubmitInput[<input type="submit" class="button primary MenuCloser" value="Post" accesskey="s">]

But I don't understand why it would be throwing an exception when it clearly has the right object. I have javascript disabled, along with css. I am using the web browser Chrome.

Edit: By right Object I mean that it's the correct button on the page that I want to click and it's in the form of a HtmlSubmitInput button.


Solution

  • I'm not sure why you think having the right object will mean you won't get any exception. You're not getting a NullPointerException, if that is what you mean by the right object.

    Having said that, if you read the exception it is a FailingHttpStatusCodeException with the following message:

    404 Not Found for http://forums.bukkit.org/threads/plugin-idea-to-get-tons-of-downloads.271185/members/jthort.90885864/post

    So that URL is giving you a 404 error when you perform an HTTP POST on it. You should keep debugging why this is happening. I would enable JavaScript, maybe it is needed there.