1

我正在用 htmlunit 测试论坛 phpbb3。我在论坛 phpbb 主题中自动添加消息时遇到问题。我需要发布隐藏输入:form_token 和 creation_time。我怎样才能得到它?

final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_7);

HtmlPage page = webClient.getPage(URL);


// get input "username" 
final HtmlElement user_name = page.getElementsByName("username").get(0);
//enter of  value               
user_name.type(par[0]);

//get input "password"

final HtmlElement password = page.getElementsByName("password").get(0);
//enter of value                
password.type(par[1]);

// get button 'login' and click

final HtmlSubmitInput submit_button = page.getElementByName("login");
page = submit_button.click();
//Go to the page of adding message              
page=page.getAnchorByText("Your first forum").click();

page=page.getAnchorByText("Welcome to phpBB3").click();

page=page.getAnchorByHref("./posting.php?mode=reply&f=2&t=1").click();
//get textarea

final HtmlTextArea myMessage=page.getElementByName("message");

/* HtmlInput form_token=page.getElementByName("form_token");

form_token.setAttribute("value", "0");

HtmlInput creation_time=page.getElementByName("creation_time");

creation_time.setAttribute("value", "0");
*/

//enter the valye               

myMessage.type("myText");

//get the button 'Submit' and click             
final HtmlElement submit_post = page.getElementByName("post");

page=submit_post.click();
//Check that my text is on the page

if (page.asText().contains("myText") {
                    System.out.println("Yes");
                }
                else System.out.println("No");

但是在此操作之后,页面上没有新消息。据我所知,服务器检查隐藏字段 form_token 和 creation_time。如何获得正确的字段值?或者你知道解决问题的其他方法吗?

4

1 回答 1

0

安东,尝试做这样的事情:

HtmlForm form = page.getFormByName("a_form");

HtmlHiddenInput formToken = form.getInputByName("form_token");
formToken.setValueAttribute("form_token_value");

/* Or without the variable: */
form.getInputByName("creation_time").setValueAttribute("creation_time_value");

HtmlSubmitInput submitInput = form.getInputByName("post");
submitInput.click();
于 2012-04-25T04:05:56.860 回答