Cheerio: :nth-child() selector returns empty result

Created on 30 Dec 2012  路  8Comments  路  Source: cheeriojs/cheerio

i know that :eq() is not implemented,
but nth-child is. (https://github.com/fb55/CSSselect/blob/master/index.js#L179)

anyways, always returns an empty result set.

Most helpful comment

I had a similar problem:

div.result_data > table > tbody > tr:nth-child(1) > td:nth-child(2)

did not work (although it matches the html markup)
This however did work:

div.result_data > table > tr:nth-child(1) > td:nth-child(2)

My theory: chrome's fault.
I rightclicked the element inside the chrome inspector and pressed 'copy css path'.
Chrome generates the CSSPath from the generated DOM (which autocorrects missing elements like <tbody> in the DOM.).

So my advice, make sure all elements are present in the actual html :)

All 8 comments

did you resolve the issue?

Yep. it was on my side. sorry for that.
the selector works perfectly

piffie, how did you solve that?

Just solved it myself:

the correct is $('.classSelector div:nth-child(n)') and not $('.classSelector:nth-child(n)')

@Hamdan85 I don't think that is corrent.

https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child

Gives an example of element:nth-child(an + b) { style properties }.

Note that it is using : before nth-child and not . as you suggest.

Did you mix this up, or is it a bug in cheerio?

oh.. im sorry... that was a misstyping. you are absolutely correct.!]

But this particular part was not the point. I just corrected the solution.

I had a similar problem:

div.result_data > table > tbody > tr:nth-child(1) > td:nth-child(2)

did not work (although it matches the html markup)
This however did work:

div.result_data > table > tr:nth-child(1) > td:nth-child(2)

My theory: chrome's fault.
I rightclicked the element inside the chrome inspector and pressed 'copy css path'.
Chrome generates the CSSPath from the generated DOM (which autocorrects missing elements like <tbody> in the DOM.).

So my advice, make sure all elements are present in the actual html :)

Firefox also seems to like insert a tbody.
More for this question on SO

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bxqgit picture bxqgit  路  3Comments

AlbertoElias picture AlbertoElias  路  4Comments

unicrus picture unicrus  路  4Comments

misner picture misner  路  3Comments

gajus picture gajus  路  4Comments