单击htmlunit中的按钮会产生强制转换异常吗?

时间:2011-07-11 12:34:39

标签: java htmlunit

当我尝试运行程序时,除了单击按钮外,一切正常。当我点击按钮时,我得到了这个例外: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());
    }
}

有没有人知道我如何解决演员问题所以它会点击表单中的按钮?

1 个答案:

答案 0 :(得分:3)

更改行

HtmlSubmitInput formSubmit = button.getInputByName("reward");

HtmlButtonInput formSubmit = button.getInputByName("reward");

如果你的HTML有

,第一行就可以了
<input type="submit" name="reward" .../>

但显然它有

<input type="button" name="reward" .../>