2014-01-05 176 views
0
public void actionVote() { 
    HtmlForm form = this.page.getForms().get(0); 
    HtmlInput button = form.getInputByValue("vote"); 
    try { 
     button.click(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

当我用button.asText()做println时,它给了我提交按钮的正确值,但是当我做button.click时,没有任何反应,就像它没有提交表单一样。Htmlunit按钮不会提交?

我无法使用HtmlButton获取按钮,因为提交按钮没有任何标识或名称。 我也无法使它从HtmlInput HtmlButton。

为什么不提交?错误的元素?

回答

0

试试这个 - getInputByNamegetButtonByName

HtmlForm form = this.page.getForms().get(0); 
HtmlInput button = form.getInputByName("vote"); 

或者你也可以创建一个假的按钮:

HtmlElement fakeButton = page.createElement("button"); 
button.setAttribute("type", "submit"); 

// add the button to the form 
form.appendChild(fakeButton); 
fakeButton.click(); 
0

你可以试试下面的代码:

HtmlForm form = page.getForms().get(0); 

HtmlElement input = form.getElementsByAttribute("input", "name", "vote").get(0); 

page = input.click(); 
+0

你好user2115021,我编辑过一个麻烦呃你的答案,以便你的代码显示为代码块。 [检查出来](http://meta.stackexchange.com/a/22189/232145),看看你如何做这种格式。它使你的发布代码更容易阅读。 –