我正在用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。如何获得正确的字段值?或者您知道解决问题的其他方法?
最佳答案
安东,尝试做这样的事情:
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();