HtmlUnit和XPath无法检索HTML元素

时间:2017-07-19 10:52:35

标签: xpath web-scraping htmlunit

我正在尝试从此网站获取输入元素:

https://www.milanuncios.com/textos-del-anuncio/?demandax=n&c=131&idlocalidad=8&p=almeria&x=27&y=0

输入本身是:

<input class="inputs campoGrande" name="marca" value="" maxlength="120" size="12" id="marca" tabindex="1" type="text">

我尝试了不同的方法,但我总是得到的是&#34; null&#34;参考:

HtmlInput inputZona = (HtmlInput) currentPage.getElementById("marca");
HtmlInput inputZona = (HtmlInput) currentPage.getFirstByXPath("//input[@name='marca']");

同样尝试了JSoup,结果相同: - (

HTMLUnit和XPAth都无法获得该项目。

这是怎么回事?

提前致谢,

1 个答案:

答案 0 :(得分:1)

此代码适用于最新版本的HtmlUnit(2.28-SNAPSHOT)。

    public static void main(String[] args) throws Exception {
        WebClient webClient = new WebClient(BrowserVersion.BEST_SUPPORTED);
        HtmlPage page = webClient.getPage("https://www.milanuncios.com/textos-del-anuncio/?demandax=n&c=131&idlocalidad=8&p=almeria&x=27&y=0");
        System.out.println(page.asXml());

        HtmlInput inputZona = (HtmlInput) page.getElementById("marca");
        System.out.println(inputZona.asXml());
    }