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

标签 java htmlunit

当我尝试运行该程序时,除了单击按钮之外,一切正常。当我单击按钮时,出现此异常: java.lang.ClassCastException: com.gargoylesoftware.htmlunit.html.HtmlButtonInput 无法转换为 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());
    }
}

有谁知道如何解决 Actor 问题,以便它会单击表单中的按钮?

最佳答案

换行

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

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

如果您的 HTML 有,第一行就可以工作

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

但显然它已经

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

关于java - 单击 htmlunit 中的按钮会产生强制转换异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6650144/

相关文章:

java - 即使启用了 javascript,HtmlUnitDrivers 向下滚动页面也无法在 java 中工作

Java 8 用供应商填充数组

java - 我无法在java中实现同一包的接口(interface)

java - Java中URI路径参数解析

java - 无法使用 HtmlUnit 点击谷歌的新 reCaptcha 复选框

java - 当页面有随机生成ID的HTML单元时获取Xpath(或元素)

Java:无法从自定义 RunLoop 实现中退出

java - 如何使用 WebServiceTemplate 发送 SOAP 请求?

java - 如何用Java实现网页截图?

java - 尝试使用 HTMLUnit 保存 html 文本框元素时出现类型转换异常