1

I am in stuck with creating cssQuery for Jsoup for parsing html.

What I want to parse:

    ...    
    <div class="counterdiv">
        <div class="counter-group" id="c1">
        <div class="counter-heading collapsed inactive-header">
        <span class="counter-number">
        <span class="rts-counter">
        <span class="rts-nr-int rts1">1</span>
        <span class="rts-nr-int rts2">2</span>
        <span class="rts-nr-int rts3">3</span>
        </span>
        </span>
        </div>
        </div>
</div>

What I want in output: 123

What I am getting in output now: null

What I've tried to get desirable output:

Element element = document.select
// Elements elements = document.select
// (".counterdiv.counter-group.counter-heading.inactive-header.rts-counter.rts-nr-int.rts1");
(".counterdiv.counter-group.counter-heading.inactive-header.counter-number span").first();
// (".counterdiv .counter-group .counter-heading inactive-header .counter-number span");
// (".counterdiv .counter-group .counter-heading inactive-header .counter-number span span span");
// ("div.counterdiv > div.counter-group > #c1 > div.counter-heading.inactive-header > span.counter-number > span");

And a lot of other such variations but element or elements always null.

Where is my problem? What I am doing wrong while building cssQuery?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
danyapd
  • 2,516
  • 1
  • 14
  • 23
  • edited my post, there were typos – danyapd May 06 '20 at 21:15
  • Anyway `.counterdiv.counter-group...` doesn't look right. We use `.class1.class2` to describe `` not ``. When you want to describe element which is a child of other element use `space` like `parentDescription childDescription`. – Pshemo May 06 '20 at 21:16
  • 1
    BUT even if those selectors can't locate such elements then maybe problem isn't with selector but with HTML response you got from server. Maybe DOM it represents doesn't contain elements you are looking for. Try printing your document and see if elements you are searching they are present there in positions you expect. If they are not then maybe they are added dynamically by JavaScript (which `jsoup` doesn't support since it isn't browser emulator but parser) – Pshemo May 06 '20 at 21:19
  • yes, seems like JavaScript dynamically adding elements. – danyapd May 06 '20 at 21:33
  • Related (or even duplicate since solution here is "to not use Jsoup but other tool which can handle JS"): [Jsoup Java HTML parser : Executing Javascript events](https://stackoverflow.com/q/7344258) – Pshemo May 06 '20 at 21:53

0 Answers0