2011-07-11 26 views
1

當我嘗試運行程序時,除了點擊按鈕,所有程序都可以運行。當我按一下按鈕我得到這個異常:java.lang.ClassCastException: com.gargoylesoftware.htmlunit.html.HtmlButtonInput cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlSubmitInput單擊htmlunit中的按鈕會產生強制異常?

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()); 
    } 
} 

有誰知道我怎麼能解決這個問題,鑄所以它會點擊窗體中的按鈕?

回答

2

更改線路

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

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

第一行會的工作,如果你的HTML有

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

但很顯然它有

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