Search code examples
javahtml-parsinghtmlunit

Cannot get value from table cell trough htmlunit when that value is number


I'm trying to parse html from page: http://www.hidmet.gov.rs/latin/prognoza/stanica.php?mp_id=13274 and cannot get value from table cell if that value is number.

\\\Tried by xpath: 

DomText dt = page.getFirstByXPath("//*[@id=\"sadrzaj\"]/div/table[1]/tr[4]/td[4]/text()"); \\ getting null

\\\Tried by reference:

String dt = (HtmlTable) page.getByXPath("//*[@id=\"sadrzaj\"]/div/table").get(0).getBodies().get(0).getRows().get(3).getCells().get(4).getTextContent();\\ getting null

Solution

  • Your XPath is wrong (misses /tbody).

    DomNode dt = page.getFirstByXPath("//*[@id=\"sadrzaj\"]/div/table[1]/tbody/tr[4]/td[4]/text()");
    System.out.println(dt);
    

    produces here 22. You can use page.asXml() to get the correct Dom structure.