Bad Hombre
Bad Hombre

Reputation: 594

Nokogiri next element by type

Say I have

<h3></h3>
<h2></h2>
<p></p>

How can I get to the p node from h3

Right now I can only get from doc.css('h3').next_element which doesn't take any arguments and returns the h2 tag.

Is there a way to check node types recursively or is there a method where I can call for example doc.css('h3').next('p')

P.S Of course the HTML I'm parsing is not as simple as the example above.

Upvotes: 1

Views: 89

Answers (1)

mechnicov
mechnicov

Reputation: 15248

If you need only one element (not collection), there is at method

And you need selector with general sibling combinator ~

doc.at('h3 ~ p')

If you need collections of such p that go after each h3 tag

doc.css('h3 ~ p')

Upvotes: 1

Related Questions