为什么HtmlButton object.click()在HtmlUnit中不起作用?

时间:2011-02-02 09:14:57

标签: eclipse junit4 htmlunit

我从一个html + javascipt页面获得了htmlbutton但是我的代码不起作用(Eclipse中的JUnit + HtmlUnit) 代码是:

final HtmlPage page = (HtmlPage)webClient.getPage(url);
final HtmlForm form = page.getFormByName("registrationForm");
final HtmlButton button=(HtmlButton)page.getElementById("submit1"); 

final HtmlTextInput phone = form.getInputByName("phoneno");

phone.setValueAttribute("1234567890");

// Now submit the form by clicking the button and get back the second page.

final HtmlPage page2 = button.click();  //this doesn't work
final String pageAsXml = page2.asText();
System.out.println(pageAsXml);

该行评论“这不起作用”有一些问题。没有错误,但它不起作用。 webclient构造函数有什么问题吗?我们需要将浏览器版本指定为参数吗?这会在eclipse中发挥作用吗?

1 个答案:

答案 0 :(得分:1)

final HtmlPage page = (HtmlPage)webClient.getPage(url);
final HtmlForm form = page.getFormByName("registrationForm");
final HtmlButton button=(HtmlButton)form.getElementById("submit1"); 
//instead of page try using the form
final HtmlTextInput phone = form.getInputByName("phoneno");

phone.setValueAttribute("1234567890");

// Now submit the form by clicking the button and get back the second page.

final HtmlPage page2 = button.click();  //this doesn't work
final String pageAsXml = page2.asText();
System.out.println(pageAsXml);

//Let me know if it works.