cheerio icon indicating copy to clipboard operation
cheerio copied to clipboard

Can´t select elements based on their position in the document

Open WilliamBelini opened this issue 2 years ago • 2 comments

From https://cheerio.js.org/docs/basics/selecting

Have a command documentation that inform you can select elements based on their position, like

const $p = $('p:first');

but if I want select $p = $('div div.first button').attr('onclick') it returns null.

if I write just $('div div button').attr('onclick'),

it returns the first value

Always I need do $('div div button').each((index,value) => $(value).attr('onclick')

(yes, the comand 'each' return the div>div elements).

And discard the 2 first elements to get my value.

I think it be more simple like $('div div[2] button').attr('onclick')

Selecting the div position with "[2]", like an array element position.

Exist some form that I can't need make the 'each' command to return the values from 3th div?

If I write like the div:second, Google App Script retuns "Error: unmatched pseudo-class :second"

Thanks gift

WilliamBelini avatar Dec 15 '23 19:12 WilliamBelini

What version of Cheerio are you using? Note that positional selectors are only available in the latest RC release.

fb55 avatar Dec 16 '23 11:12 fb55

I'm using the last version avaible from Google App Script

Em sáb., 16 de dez. de 2023 08:07, Felix Boehm @.***> escreveu:

What version of Cheerio are you using? Note that positional selectors are only available in the latest RC release.

— Reply to this email directly, view it on GitHub https://github.com/cheeriojs/cheerio/issues/3547#issuecomment-1858791074, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE7LFVZJSCF2MOS4RDFLIQDYJV6F3AVCNFSM6AAAAABAW366L6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNJYG44TCMBXGQ . You are receiving this because you authored the thread.Message ID: @.***>

WilliamBelini avatar Dec 16 '23 15:12 WilliamBelini

I cannot find a version of Cheerio in Google App Script, so closing this as cannot reproduce. Please let me know if you have further details!

fb55 avatar Aug 08 '24 08:08 fb55

image

link to libraire> https://github.com/tani/cheeriogs It is very simple. I thing couldn't explained easily.

Imagine you have a link in a button, and you want to select it. The code show like that

<div>
   <div>
   <div>
   <div>
         <button onclick: "I dont want this link">
         <button onclick: "My beautiful link">

The unique form I can use to do it (select my beautiful link) is running a loop, using the function "each". I wanna write something like this $(div div[3] button[2]) to don't need run a loop always.

WilliamBelini avatar Aug 09 '24 00:08 WilliamBelini

Thanks for the link! cheeriogs is using a version of cheerio that does not support positional selectors. Once it upgrades to the latest version, you'll be able to use :eq(2) to select the second element.

cc @tani

fb55 avatar Aug 09 '24 07:08 fb55

Noted with thanks. I'll update it. After the deployment, I am going to post the message here as soon as possible.

tani avatar Aug 09 '24 09:08 tani

Hi, v15 is available now. You can use 1.0.0-rc12 on Google Apps Script. Cheers.

tani avatar Aug 09 '24 14:08 tani

Hi,thank youEm 9 de ago. de 2024 10:32, TANIGUCHI Masaya @.***> escreveu: Hi, v15 is available now. You can use 1.0.0-rc12 on Google Apps Script. Cheers.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

WilliamBelini avatar Aug 09 '24 23:08 WilliamBelini