When I try to run the program everything works except clicking the button. When I click the button I get this exception: java.lang.ClassCastException: com.gargoylesoftware.htmlunit.html.HtmlButtonInput cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlSubmitInput
public class Connect {
public Connect(int port, String host) {
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3/*, host, port*/);
webClient.setJavaScriptEnabled(true);
HtmlPage page = null;
try {
page = webClient.getPage("localhost/vote.php");
} catch (IOException e) {
e.printStackTrace();
}
HtmlForm button = page.getFormByName("voted");
HtmlSubmitInput formSubmit开发者_开发技巧 = button.getInputByName("reward");//errors: java.lang.ClassCastException: com.gargoylesoftware.htmlunit.html.HtmlButtonInput cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlSubmitInput
page.executeJavaScript("setStatus(1);");
page.executeJavaScript("setStatus(2);");
page.executeJavaScript("setStatus(3);");
page.executeJavaScript("canClickReward = true;");
try {
formSubmit.click();
} catch (IOException e) {
System.out.println("Form Button" + e.getMessage());
}
//page.executeJavaScript("document.forms[\"voted\"].submit()"); //Doesn't submit form
System.out.println(page.asText());
}
}
Does anyone know how I could fix the cast issue so it will click the button in the form?
Change the line
HtmlSubmitInput formSubmit = button.getInputByName("reward");
to
HtmlButtonInput formSubmit = button.getInputByName("reward");
The first line would work if your HTML had
<input type="submit" name="reward" .../>
But apparently it has
<input type="button" name="reward" .../>
精彩评论