floki icon indicating copy to clipboard operation
floki copied to clipboard

Selecting parents

Open glyh opened this issue 1 year ago • 1 comments

Feature goal

When I encounter some webpage like this:

<x><y>hey</y> wat</x>

I want to select the tag x, and get the text " wat". The characteristic for this tag is that it's a parent of some other tag containing text "hey".

There doesn't seem to be one help me to do so. For now I just use "E:nth-of-type" to find it by index, but this seems fragile as once the element moves this will break.

Dependencies

None

glyh avatar Oct 01 '24 13:10 glyh

This was fixed in #624 with the implementation of the :has pseudo-selector, should be released in v0.38 soon.

bvobart avatar Jun 06 '25 13:06 bvobart

As @bvobart mentioned, we now have the :has pseudo-selector (from v0.38). I think you can work with a combination of :has(y:fl-contains('hey')) and Floki.text() with the :deep option set to false. See the docs: https://hexdocs.pm/floki/Floki.html#text/2

I'm going to close this one, but feel free to comment if you have any questions.

philss avatar Jun 15 '25 03:06 philss