gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 06:20:26 24 4
gpt4 key购买 nike

当我尝试运行该程序时,除了单击按钮外,一切正常。当我单击按钮时,出现此异常: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());
}
}

有谁知道我如何解决 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com