Shlomi Bazel Shlomi Bazel - 5 months ago 35
CSS Question

JSoup CSS / DOM questions


I want to get the text in the picture, in Google Developer Tools I would do that (I basically went into another childnode of the span to find the md5 in DevTools but in Jsoup it seems different and only returns the "md5" text)


I cant manage to get it using JSoup dom/selector.
(If it's possible to give both of these examples)


How do I specify a child in CSS in Jsoup?
For example, I right click on the span class field above the blue marked line in the picture, and click "Copy Selector":

#file-details > div:nth-child(2) > div:nth-child(1) > span

It gives me file-details as first div, even thought its not the only file-details in the document, but okay, lets say it should be like that(?):

#additional-info-content > div:file-details > div:nth-child(2) > div:nth-child(1) > span

How do I manage to translate it into a working JSoup CSS script with the child? (If possible then DOM example aswell)


Is there a good insight on how to look and how to find the right path when looking for a specific value/node?

What I do now is basically open Developer Tools, then click on a unique div class name, and I check the properties window inside the DevTools for the child nodes, and keep digging with the child nodes till I find the right path...(Like I copied in the first question)

Is there a better way to look at this?

I mean, using the DevTools console is so simple, just writing
.children[1].childnodes[3].children[1] while looking at the properties and seeing the correct attribute that I need, but I know it's not the right way I guess?

Answer Source


    // connect to url and retrieve source code as document
    Document doc = Jsoup

    String md5= doc

            // use CSS selector to grab only enums which contain md5
            .select("div#file-details.extra-info > div.enum-container > div.enum:contains(md5)")

            // use the first element in the result set

            // use only its text node and ignore the text node of the span

2) There are lots of ways to specify children. You can use CSS selectors or some of the jsoup convenience methods.

If I want to extract the text foo from the following html:


Each of these will produce the same result:"div > span > b").last().ownText();"div > span > b").get(1).ownText();"div > span:last-child > b").text();"div > span:last-child").text();"div > span").last().text();"div > span").get(1).text();"div > span:last-child > b").first().ownText();"span > b").last().text();

Deciding which way to go really depends on the HTML structure of the document you are parsing. See CSS Selectors for more examples.

3) Examine the source code, not the code rendered in the browser. Jsoup does not invoke JavaScript. If the DOM of your page is changed onLoad, then you need to render the page before parsing it. Here is an example of how to do this:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download