jpw
jpw

Reputation: 19247

How do I find all HTML <span> elements with an attribute foo that equals a value bar?

I've found lots of Nokogiri examples of how to use XPath, or CSS, to find nodes with a particular attribute 'foo'.

I'm not quite getting how to find all the node where a certain attribute equals a certain value.

Specifically, given a HTML doc that might contain multiple instances of:

<span class='irrelevant' attrFoo='valueBar'>Some Text</span>

How would I find them using Nokogiri?

I thought this would do the trick:

doc.xpath("//span[contains(@attrFoo, 'valueBar')]")

But it returns an empty array.

Upvotes: 0

Views: 354

Answers (1)

jpw
jpw

Reputation: 19247

Doh. The XPath in my question works perfectly. I had the wrong case for the attribute, e.g., it should have used @attrFoo but I had @attrfoo.

Upvotes: 0

Related Questions