【问题标题】:HtmlUnit error will not get second page after javascript?javascript后HtmlUnit错误不会得到第二页?
【发布时间】:2013-04-13 18:19:55
【问题描述】:

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

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!

【问题讨论】:

  • 能贴出html代码吗?这是java代码吗?
  • 是htmlscripter生成的java。
  • NPE 的位置意味着没有 id="ctl00_phContent_ucUnifiedSearch_txtIndvl" 的元素
  • @Lee 你是对的。问题是 htmlunit 不等待页面加载。我不得不使用硒来避免这种问题

标签: htmlunit


【解决方案1】:

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

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 的元素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多