1

I know you can search text in html using wildcards. Can you search for attribute values in html using wildcards with nokogiri

e.g., suppose I want to search for classes with value *session*

0

2 Answers 2

4

You can use xpath contains() function to search the document. Something like:

doc.xpath("//*[@*[contains(., 'session')]]").each do |ele|
  # something
end

This search returns all the elements with any attribute whose value contains the string 'session'.

2
  • 1
    Since he specifically wants classes, @class may be better than @* Jul 21, 2012 at 19:13
  • Note that using contains() alone like this will match class="bad-session".
    – Phrogz
    Jul 22, 2012 at 16:19
1

Had a similar problem few days ago - notice spaces around class values.

find(:xpath, "//*[contains(concat(' ', normalize-space(@class), ' '), ' icon-edit ')]")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.