Search code examples
javahtmljsoup

Get n-th child Element with Jsoup


For example a web site has a code like this:

<div>
    <div>
        first
    </div>
    <div>
        second
    </div>
    <div>
        third
    </div>
</div>

and I want to get the "second" div text with "Jsoup" and it has no attribute or class.


Solution

  • There are few ways to do it.

    For instance we could use select method which returns Elements with all specified elements. Since Elements extends ArrayList<Element> it inherits all of ArrayList public methods. This means we can use get(index) method to select specific child (starting from 0)

     String html = 
            "<div>\n" +
            "    <div>\n" +
            "        first\n" +
            "    </div>\n" +
            "    <div>\n" +
            "        second\n" +
            "    </div>\n" +
            "    <div>\n" +
            "        third\n" +
            "    </div>\n" +
            "</div>";
    Document doc = Jsoup.parse(html);
    Elements select = doc.select("div > div");
    System.out.println(select.get(1));
    

    Output:

    <div>
      second 
    </div>
    

    Another way could be using :eq(n) in CSS selector (from official Jsoup tutorial)

    :eq(n): find elements whose sibling index is equal to n; e.g. form input:eq(1)

    like

    System.out.println(doc.select("div > div:eq(1)"));