在javascript之后HtmlUnit错误不会获得第二页?

时间:2013-03-30 14:44:15

标签: htmlunit

有人请解释为什么会出现空指针异常

HtmlPage page = null;
boolean savePagesLocally = false;
String url = "http://example.com";

WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setThrowExceptionOnScriptError(false);
try
{
    page = webClient.getPage( url );

    HtmlRadioButtonInput radioButton2 = (HtmlRadioButtonInput) page.getElementById("ctl00_phContent_ucUnifiedSearch_rdoIndvl");
    radioButton2.click();

    HtmlTextInput textField3 = (HtmlTextInput) page.getElementById("ctl00_phContent_ucUnifiedSearch_txtIndvl");
    textField3.setValueAttribute("1061726"); // null pointer occurs here!

1 个答案:

答案 0 :(得分:0)

对于那些想知道的人: 替换行

HtmlTextInput textField3 = (HtmlTextInput) page.getElementById("ctl00_phContent_ucUnifiedSearch_txtIndvl");

使用以下循环:

HtmlTextInput textField3 = (HtmlTextInput) page.getElementById("ctl00_phContent_ucUnifiedSearch_txtIndvl");
while(textField3 != null) {
  webClient.waitForBackgroundJavaScript(100)
  textField3 = (HtmlTextInput) page.getElementById("ctl00_phContent_ucUnifiedSearch_txtIndvl");
}

此循环将等到HTMLUnit具有该ID的元素。